﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Inventory;
using Styx.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;

namespace TuanHA_Hunter
{
    public partial class Classname
    {
        #region AimedShotMasterMarksman

        private Composite AimedShotMasterMarksman()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AimedShot &&
                SpellManager.HasSpell("Aimed Shot") &&
                Me.GetAuraById(82926) != null &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Aimed Shot", Me.CurrentTarget, false, false),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Aimed Shot", Me.CurrentTarget, "AimedShotMasterMarksman");
                        })
                );
        }

        #endregion

        #region AimedShotCapFocus

        private static bool CanAimedShotCapFocus()
        {
            if (THSettings.Instance.AimedShot &&
                SpellManager.HasSpell("Aimed Shot") &&
                PlayerFocus > 80 &&
                (HasGlyph.Contains("126095") ||
                 !Me.IsMoving) &&
                SpellManager.CanCast("Aimed Shot", Me.CurrentTarget, false, false) &&
                !SpellManager.CanCast("Chimera Shot"))
            {
                return true;
            }
            return false;
        }

        private Composite AimedShotCapFocus()
        {
            return new Decorator(
                ret =>
                ArcaneCapLast + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                Attackable(Me.CurrentTarget, 40) &&
                !Me.CurrentTarget.IsPlayer &&
                FacingOverride(Me.CurrentTarget) &&
                CanAimedShotCapFocus(),
                new Action(
                    ret =>
                        {
                            ArcaneCapLast = DateTime.Now;
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Aimed Shot", Me.CurrentTarget, "AimedShotCapFocus");
                        })
                );
        }

        #endregion

        #region AMurderofCrows

        private Composite AMurderofCrows()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AMurderofCrows &&
                (THSettings.Instance.AMoCLRCD ||
                 THSettings.Instance.AMoCLRCBurst &&
                 Burst) &&
                SpellManager.HasSpell("A Murder of Crows") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                !MyAura("A Murder of Crows", Me.CurrentTarget) &&
                (Me.CurrentTarget.IsPlayer ||
                 Me.CurrentTarget.CurrentHealth > Me.MaxHealth ||
                 IsDummy(Me.CurrentTarget)) &&
                //PlayerFocus >= 60 &&
                GetSpellCooldown("A Murder of Crows", 1000) <= TimeSpan.FromMilliseconds(400),
                //SpellManager.CanCast("A Murder of Crows"),
                new Action(
                    ret =>
                        {
                            if (PlayerFocus < 60)
                            {
                                if (SpellManager.HasSpell("Cobra Shot"))
                                {
                                    CastSpell("Cobra Shot", Me.CurrentTarget, "AMurderofCrows: Cobra Shot");
                                }
                                else
                                {
                                    CastSpell("Steady Shot", Me.CurrentTarget, "AMurderofCrows: Steady Shot");
                                }
                            }
                            else
                            {
                                CastSpell("A Murder of Crows", Me.CurrentTarget, "AMurderofCrows");
                            }
                        })
                );
        }

        #endregion

        #region ArcaneShot

        private Composite ArcaneShot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ArcaneShot &&
                SpellManager.HasSpell("Arcane Shot") &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverrideWide(Me.CurrentTarget) &&
                SpellManager.CanCast("Arcane Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Arcane Shot", Me.CurrentTarget, "ArcaneShot");
                        })
                );
        }

        #endregion

        #region ArcaneShotBM

        private Composite ArcaneShotBM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ArcaneShot &&
                SpellManager.HasSpell("Arcane Shot") &&
                !Me.Mounted &&
                (PlayerFocus > BeastWithinFocus(60) ||
                 Me.GetAuraById(34720) != null) &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverrideWide(Me.CurrentTarget) &&
                SpellManager.CanCast("Arcane Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Arcane Shot", Me.CurrentTarget, "ArcaneShotBM");
                        })
                );
        }

        #endregion

        #region ArcaneShotSV

        private Composite ArcaneShotSV()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ArcaneShot &&
                SpellManager.HasSpell("Arcane Shot") &&
                !Me.Mounted &&
                (PlayerFocus >= 45 ||
                 Me.GetAuraById(34720) != null) &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverrideWide(Me.CurrentTarget) &&
                SpellManager.CanCast("Arcane Shot") &&
                !SpellManager.CanCast("Explosive Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Arcane Shot", Me.CurrentTarget, "ArcaneShotSV");
                            //}
                        })
                );
        }

        #endregion

        #region ArcaneShotCapFocusSV

        private DateTime ArcaneCapLast;

        private Composite ArcaneShotCapFocusSV()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ArcaneShot &&
                SpellManager.HasSpell("Arcane Shot") &&
                !Me.Mounted &&
                (PlayerFocus > 80 &&
                 ArcaneCapLast + TimeSpan.FromMilliseconds(1500) < DateTime.Now ||
                 Me.GetAuraById(34720) != null) && //Thrill of the Hunt&&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverrideWide(Me.CurrentTarget) &&
                SpellManager.CanCast("Arcane Shot"),
                new Action(
                    ret =>
                        {
                            ArcaneCapLast = DateTime.Now;
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Arcane Shot", Me.CurrentTarget, "ArcaneShotCapFocusSV");
                        })
                );
        }

        private Composite ArcaneShotCapFocusBM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ArcaneShot &&
                ArcaneCapLast + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                SpellManager.HasSpell("Arcane Shot") &&
                !Me.Mounted &&
                (PlayerFocus > 80 &&
                 ArcaneCapLast + TimeSpan.FromMilliseconds(1500) < DateTime.Now ||
                 Me.GetAuraById(34720) != null) && //Thrill of the Hunt&&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverrideWide(Me.CurrentTarget) &&
                SpellManager.CanCast("Arcane Shot"),
                new Action(
                    ret =>
                        {
                            ArcaneCapLast = DateTime.Now;
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Arcane Shot", Me.CurrentTarget, "ArcaneShotCapFocusBM");
                        })
                );
        }

        #endregion

        #region ArcaneShotMM

        private Composite ArcaneShotMM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ArcaneShot &&
                SpellManager.HasSpell("Arcane Shot") &&
                !Me.Mounted &&
                (PlayerFocus > 70 &&
                 !CanAimedShotCapFocus() ||
                 Me.GetAuraById(34720) != null) && //Thrill of the Hunt
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverrideWide(Me.CurrentTarget) &&
                SpellManager.CanCast("Arcane Shot") &&
                !SpellManager.CanCast("Chimera Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Arcane Shot", Me.CurrentTarget, "ArcaneShotMM");
                        })
                );
        }

        #endregion

        #region Aspect

        // [Aspect of the Cheetah] [Aspect of the Hawk] [Aspect of the Pack] [Aspect of the Iron Hawk]
        private Composite Aspect()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoAspect &&
                    !Me.Mounted &&
                    !Me.Combat &&
                    SpellManager.HasSpell("Aspect of the Iron Hawk") &&
                    Me.HasAura("Aspect of the Hawk") &&
                    SpellManager.CanCast("Aspect of the Iron Hawk"),
                    new Action(
                        ret =>
                            {
                                CastSpell("Aspect of the Iron Hawk", Me, "Aspect of the Iron Hawk");
                                return RunStatus.Failure;
                            })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoAspect &&
                    SpellManager.HasSpell("Aspect of the Cheetah") &&
                    (TreeRoot.Current.Name == "Questing" ||
                     TreeRoot.Current.Name == "Grind Bot") &&
                    !Me.Mounted &&
                    !Me.Combat &&
                    Me.IsMoving &&
                    !Me.HasAura("Aspect of the Cheetah") &&
                    !Me.HasAura("Aspect of the Pack") &&
                    Me.CurrentTarget == null &&
                    SpellManager.CanCast("Aspect of the Cheetah"),
                    new Action(
                        ret =>
                            {
                                CastSpell("Aspect of the Cheetah", Me, "Aspect Run");
                                return RunStatus.Failure;
                            })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoAspect &&
                    //!Me.Mounted &&
                    //(TreeRoot.Current.Name == "Questing" ||
                    // TreeRoot.Current.Name == "Grind Bot") &&
                    //!Me.Mounted &&
                    !Me.HasAura("Aspect of the Iron Hawk") &&
                    !Me.HasAura("Aspect of the Hawk") &&
                    (Attackable(Me.CurrentTarget, 25) ||
                     Me.Mounted) &&
                    SpellManager.CanCast("Aspect of the Hawk"),
                    new Action(
                        ret =>
                            {
                                if (SpellManager.HasSpell("Aspect of the Iron Hawk"))
                                {
                                    CastSpell("Aspect of the Iron Hawk", Me, "Aspect Combat");
                                }
                                else
                                {
                                    CastSpell("Aspect of the Hawk", Me, "Aspect Combat");
                                }
                                return RunStatus.Failure;
                            })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoAspect &&
                    !Me.Mounted &&
                    //!Me.Mounted &&
                    (!Me.HasAura("Aspect of the Cheetah") &&
                     !Me.HasAura("Aspect of the Hawk") &&
                     !Me.HasAura("Aspect of the Pack") &&
                     !Me.HasAura("Aspect of the Iron Hawk") ||
                     Me.HasAura("Dazed") &&
                     !Me.HasAura("Aspect of the Hawk") &&
                     !Me.HasAura("Aspect of the Iron Hawk")) &&
                    SpellManager.CanCast("Aspect of the Hawk"),
                    new Action(
                        ret =>
                            {
                                if (SpellManager.HasSpell("Aspect of the Iron Hawk"))
                                {
                                    CastSpell("Aspect of the Iron Hawk", Me, "Aspect Daze");
                                }
                                else
                                {
                                    CastSpell("Aspect of the Hawk", Me, "Aspect Daze");
                                }
                                return RunStatus.Failure;
                            })
                    )
                );
        }

        #endregion

        #region AutoTarget

        private Composite AutoTarget()
        {
            return new Action(delegate
                {
                    //target the lowest threat unit
                    if (THSettings.Instance.AutoTarget &&
                        Me.CurrentTarget != null && Me.CurrentTarget.IsValid &&
                        !AttackableNoLoS(Me.CurrentTarget, 50) &&
                        //(!Attackable(Me.CurrentTarget, 50) ||
                        // Me.CurrentTarget.GetThreatInfoFor(Me).RawPercent > 90) &&
                        GetBestTarget() &&
                        UnitBestTarget.IsValid && Me.CurrentTarget != UnitBestTarget)
                    {
                        UnitBestTarget.Target();
                        Logging.Write(LogLevel.Diagnostic, "Switch to Best Unit");
                    }

                    //target the lowest threat unit
                    if (THSettings.Instance.AutoTarget &&
                        (Me.CurrentTarget == null ||
                         !AttackableNoLoS(Me.CurrentTarget, 50)) &&
                        GetBestTarget() &&
                        UnitBestTarget.IsValid)
                    {
                        UnitBestTarget.Target();
                        Logging.Write(LogLevel.Diagnostic, "Target  Best Unit");
                    }

                    //Hold dps on Dungeon 
                    //if ((Me.CurrentMap.IsDungeon ||
                    //     Me.CurrentMap.IsRaid) &&
                    //    !Me.Combat)// &&
                    //Me.CurrentTarget != null &&
                    //IsEnemy(Me.CurrentTarget) &&
                    //!Me.CurrentTarget.IsTargetingMyPartyMember &&
                    //!Me.CurrentTarget.IsTargetingMyRaidMember &&
                    //!Me.CurrentTarget.IsTargetingMeOrPet)
                    //{
                    //    return RunStatus.Success;
                    //}

                    if (THSettings.Instance.AutoAoE &&
                        THSettings.Instance.UnittoStartAoE > 0 &&
                        Attackable(Me.CurrentTarget, 40) &&
                        CountEnemyNear(Me.CurrentTarget, 10) >= THSettings.Instance.UnittoStartAoE)
                    {
                        _aoEModeOn = true;
                    }
                    else
                    {
                        _aoEModeOn = false;
                    }

                    return RunStatus.Failure;
                });
        }

        #endregion

        #region Barrage

        private Composite Barrage()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Barrage &&
                SpellManager.HasSpell("Barrage") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                //(target.IsPlayer ||
                // target.CurrentHealth > Me.MaxHealth * 0.5 ||
                // IsDummy(target)) &&
                SpellManager.CanCast("Barrage"),
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Barrage", Me.CurrentTarget, "Barrage");
                        })
                );
        }

        #endregion

        #region BestialWrath

        private Composite BestialWrath()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BestialWrath &&
                SpellManager.HasSpell("Bestial Wrath") &&
                !Me.HasAura("Bestial Wrath") &&
                !Me.Mounted &&
                Me.GotAlivePet &&
                Me.Pet.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                //(target.IsPlayer ||
                // target.CurrentHealth > Me.MaxHealth*5 &&
                // target.MaxHealth > Me.MaxHealth*10 ||
                // IsDummy(target)) &&
                (THSettings.Instance.BestialWrathCD &&
                 SpellManager.HasSpell("Kill Command") &&
                 GetSpellCooldown("Kill Command") < TimeSpan.FromMilliseconds(600) ||
                 THSettings.Instance.BestialWrathBurst &&
                 Burst &&
                 SpellManager.HasSpell("Kill Command") &&
                 GetSpellCooldown("Kill Command") < TimeSpan.FromMilliseconds(600) ||
                 THSettings.Instance.BestialWrathLC &&
                 SpellManager.HasSpell("Kill Command") &&
                 DebuffCC(Me.Pet) && LastCastSpell != "Bullheaded") &&
                //!IsUnitsInLosOfEachOtherAtFeet(Me.Pet, target) &&
                SpellManager.CanCast("Bestial Wrath"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Bestial Wrath", Me.CurrentTarget, "BestialWrath");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region BeastWithinFocus

        private static double BeastWithinFocus(int currentFocus)
        {
            if (Me.HasAura("The Beast Within"))
            {
                return currentFocus*0.5;
            }
            return currentFocus;
        }

        #endregion

        #region Black Arrow

        private static WoWUnit UnitBlackArrow;

        private static bool GetUnitUnitBlackArrow()
        {
            UnitBlackArrow = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitBlackArrow = (from unit in NearbyUnFriendlyUnits
                                  where unit.IsValid
                                  where MyAura("Black Arrow", unit)
                                  select unit).FirstOrDefault();
            }
            return UnitBlackArrow != null;
        }

        private Composite BlackArrow()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlackArrow &&
                SpellManager.HasSpell("Black Arrow") &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 40) &&
                (Me.CurrentTarget.IsPlayer ||
                 Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.5 ||
                 IsDummy(Me.CurrentTarget)) &&
                !MyAura("Black Arrow", Me.CurrentTarget) &&
                SpellManager.CanCast("Black Arrow"),
                new Action(
                    ret =>
                        {
                            CastSpell("Black Arrow", Me.CurrentTarget, "BlackArrow");
                            UnitBlackArrow = Me.CurrentTarget;
                            SafelyFacingTarget(Me.CurrentTarget);
                        })
                );
        }

        #endregion

        #region BlinkStrike

        private static DateTime LastPetRangAttack;

        //private static DateTime LastBlinkStrike;

        private Composite BlinkStrike()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlinkStrike &&
                LastPetRangAttack + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                HoldPetAction + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Blink Strike") &&
                Me.GotAlivePet &&
                !Me.Mounted &&
                AttackableNoLoS(Me.CurrentTarget, 80) &&
                SpellManager.CanCast("Blink Strike"),
                new Action(ret =>
                    {
                        LastPetRangAttack = DateTime.Now;
                        if (Me.Pet.CurrentTarget == null ||
                            Me.Pet.CurrentTarget != null &&
                            Me.Pet.CurrentTarget != Me.CurrentTarget)
                        {
                            Lua.DoString("RunMacroText('/PetAttack');");
                        }
                        CastSpell("Blink Strike", Me.CurrentTarget, "BlinkStrike");
                    }));
        }

        private Composite BlinkStrikeRange()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BlinkStrike &&
                LastPetRangAttack + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                HoldPetAction + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Blink Strike") &&
                Me.GotAlivePet &&
                !Me.Mounted &&
                AttackableNoLoS(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.IsPlayer &&
                Me.Pet.Location.Distance(Me.CurrentTarget.Location) > 20 &&
                SpellManager.CanCast("Blink Strike"),
                new Action(ret =>
                    {
                        LastPetRangAttack = DateTime.Now;
                        if (Me.Pet.CurrentTarget == null ||
                            Me.Pet.CurrentTarget != null &&
                            Me.Pet.CurrentTarget != Me.CurrentTarget)
                        {
                            Lua.DoString("RunMacroText('/PetAttack');");
                        }
                        CastSpell("Blink Strike", Me.CurrentTarget, "BlinkStrikeRange");
                    }));
        }

        #endregion

        #region BullHeaded

        private Composite BullHeaded()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.BullHeaded &&
                LastCastSpell != "Bestial Wrath" &&
                Me.Combat &&
                Me.GotAlivePet &&
                (DebuffCCDurationPet(Me.Pet, 3000) ||
                 DebuffRootPet(Me.Pet)) &&
                PetCanCastAction("Bullheaded"),
                new Action(
                    ret =>
                        {
                            LastCastSpell = "Bullheaded";
                            Lua.DoString("RunMacroText('/Cast Bullheaded');");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region CalculateDropLocation

        private static WoWPoint CalculateDropLocation(WoWUnit target)
        {
            WoWPoint dropLocation;

            if (!target.IsMoving || target.MovementInfo.MovingBackward)
            {
                dropLocation = target.Location;
            }
            else
            {
                dropLocation = WoWMathHelper.CalculatePointBehind(target.Location, target.Rotation,
                                                                  -(8 + target.MovementInfo.RunSpeed));
            }

            return dropLocation;
        }

        #endregion

        #region CallPet

        private static DateTime LastCallPet;
        private static DateTime LastPhoenix;
        private static DateTime LastRevive;
        private static DateTime LastCallPetAction;

        private Composite CallPet()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.AutoCallPet &&
                !Me.Mounted &&
                !Me.InVehicle &&
                !Me.GotAlivePet,
                new Action(
                    ret =>
                        {
                            if (LastCallPet + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                                LastCallPetAction + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                                !Me.GotAlivePet)
                            {
                                //Logging.Write("Try to Call Pet");
                                if (Me.Specialization == WoWSpec.HunterMarksmanship)
                                {
                                    Lua.DoString("RunMacroText('/Cast Call Pet " + THSettings.Instance.CallPetNumberMM +
                                                 "');");
                                }
                                else if (Me.Specialization == WoWSpec.HunterSurvival)
                                {
                                    Lua.DoString("RunMacroText('/Cast Call Pet " + THSettings.Instance.CallPetNumberSV +
                                                 "');");
                                }
                                else
                                {
                                    Lua.DoString("RunMacroText('/Cast Call Pet " + THSettings.Instance.CallPetNumber +
                                                 "');");
                                }
                                LastCallPet = DateTime.Now;
                                LastCallPetAction = DateTime.Now;
                            }

                            else if (LastPhoenix + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                                     LastCallPetAction + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                                     !Me.GotAlivePet &&
                                     Me.Combat &&
                                     PetCanCastAction("Heart of the Phoenix"))
                            {
                                //Logging.Write("Try to Cast Heart of the Phoenix");
                                Lua.DoString("RunMacroText('/Cast Heart of the Phoenix');");
                                LastPhoenix = DateTime.Now;
                                LastCallPetAction = DateTime.Now;
                            }
                            else if (LastRevive + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                                     LastCallPetAction + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                                     !Me.GotAlivePet &&
                                     SpellManager.CanCast("Revive Pet"))
                            {
                                //Logging.Write("Try to Cast Revive");

                                while (Me.IsMoving)
                                {
                                    WoWMovement.MoveStop();
                                }
                                CastSpell("Revive Pet", Me);
                                LastRevive = DateTime.Now;
                                LastCallPetAction = DateTime.Now;
                                while (Me.IsCasting)
                                {
                                }
                            }

                            if (!Me.Combat && !Me.GotAlivePet)
                            {
                                return RunStatus.Success;
                            }
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region Chimera Shot

        private Composite ChimeraShot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ChimeraShot &&
                SpellManager.HasSpell("Chimera Shot") &&
                !Me.Mounted &&
                PlayerFocus >= 45 &&
                Attackable(Me.CurrentTarget, 40) &&
                //SpellManager.CanCast("Chimera Shot"),
                SpellManager.CanCast("Chimera Shot"),
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            SafelyFacingTarget(Me.CurrentTarget);
                            //while (SpellManager.Spells["Chimera Shot"].CooldownTimeLeft.TotalMilliseconds < 1500)
                            //{
                            CastSpell("Chimera Shot", Me.CurrentTarget, "ChimeraShot");
                            LastSnare = DateTime.Now;
                            //}
                            //}
                            //else if (!Casting())
                            //{
                            //    CastSpell("Steady Shot", Me.CurrentTarget, "ChimeraShot: Steady Shot");
                            //}
                        })
                );
        }

        #endregion

        #region ChimeraShotPvP

        //private Composite ChimeraShotPvP()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.ChimeraShot &&
        //        SpellManager.HasSpell("Chimera Shot") &&
        //        !Me.Mounted &&
        //        Attackable(Me.CurrentTarget, 40) &&
        //        Me.CurrentTarget.IsPlayer &&
        //        (PlayerFocus >= 45 || LastCastSpell == "Steady Shot") &&
        //        GetSpellCooldown("Chimera Shot") <= 1,
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Chimera Shot", Me.CurrentTarget, "ChimeraShot");
        //                    SafelyFacingTarget(Me.CurrentTarget);
        //                    LastSnare = DateTime.Now;
        //                })
        //        );
        //}

        #endregion

        #region CobraShotBM

        private Composite CobraShotBM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.CobraShot &&
                LastCastSpell != "Fervor" &&
                SpellManager.HasSpell("Cobra Shot") &&
                !Me.Mounted &&
                PlayerFocus < BeastWithinFocus(60) &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Cobra Shot", Me.CurrentTarget, false, false),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Cobra Shot", Me.CurrentTarget, "CobraShotBM");
                        })
                );
        }

        #endregion

        #region CobraShotBMBuildFocus

        private Composite CobraShotBMBuildFocus()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.CobraShot &&
                LastCastSpell != "Fervor" &&
                SpellManager.HasSpell("Cobra Shot") &&
                !Me.Mounted &&
                PlayerFocus < BeastWithinFocus(40) &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Cobra Shot", Me.CurrentTarget, false, false),
                new Action(
                    ret =>
                    {
                        SafelyFacingTarget(Me.CurrentTarget);
                        CastSpell("Cobra Shot", Me.CurrentTarget, "CobraShotBMBuildFocus");
                    })
                );
        }

        #endregion

        #region CobraShotRefreshSting

        //private Composite CobraShotRefreshSting()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.CobraShot &&
        //        SpellManager.HasSpell("Cobra Shot") &&
        //        LastCastSpell != "Cobra Shot" &&
        //        LastCastSpell != "Serpent Sting" &&
        //        LastRefeshSting + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
        //        !Me.Mounted &&
        //        PlayerFocus < BeastWithinFocus(75) &&
        //        Attackable(Me.CurrentTarget, 40) &&
        //        MyAuraTimeLeft("Serpent Sting", Me.CurrentTarget) > 3000 &&
        //        MyAuraTimeLeft("Serpent Sting", Me.CurrentTarget) < 9000 &&
        //        FacingOverride(Me.CurrentTarget) &&
        //        SpellManager.CanCast("Cobra Shot", Me.CurrentTarget, false, false),
        //        new Action(
        //            ret =>
        //                {
        //                    LastRefeshSting = DateTime.Now;
        //                    SafelyFacingTarget(Me.CurrentTarget);
        //                    CastSpell("Cobra Shot", Me.CurrentTarget, "CobraShotRefreshSting");
        //                })
        //        );
        //}

        #endregion

        #region CobraShotSV

        private Composite CobraShotSV()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.CobraShot &&
                LastCastSpell != "Fervor" &&
                SpellManager.HasSpell("Cobra Shot") &&
                !Me.Mounted &&
                PlayerFocus < 60 &&
                //Me.GetAuraById(56453) == null &&//"Lock and Load"
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Cobra Shot", Me.CurrentTarget, false, false) &&
                !SpellManager.CanCast("Explosive Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Cobra Shot", Me.CurrentTarget, "CobraShotSV");
                        })
                );
        }

        #endregion

        #region CobraShotAoESV

        private Composite CobraShotAoEBM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.CobraShot &&
                LastCastSpell != "Fervor" &&
                SpellManager.HasSpell("Cobra Shot") &&
                !Me.Mounted &&
                PlayerFocus < 60 &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Cobra Shot", Me.CurrentTarget, "CobraShotAoEBM");
                        })
                );
        }

        #endregion

        #region CobraShotAoESV

        private Composite CobraShotAoESV()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.CobraShot &&
                LastCastSpell != "Fervor" &&
                SpellManager.HasSpell("Cobra Shot") &&
                !Me.Mounted &&
                Me.GetAuraById(56453) == null && //"Lock and Load"
                PlayerFocus < 60 &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Cobra Shot", Me.CurrentTarget, "CobraShotAoESV");
                        })
                );
        }

        #endregion

        #region Concussive Shot

        private static DateTime LastSnare;

        private Composite ConcussiveShot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ConcussiveShot &&
                LastSnare + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Concussive Shot") &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.IsMoving &&
                Me.CurrentTarget.IsPlayer &&
                //FacingOverrideWide(target) &&
                (Me.CurrentTarget.Mounted ||
                 !DebuffRootorSnare(Me.CurrentTarget)) &&
                !InvulnerableRootandSnare(Me.CurrentTarget) &&
                SpellManager.CanCast("Concussive Shot"),
                new Action(
                    ret =>
                        {
                            CastSpell("Concussive Shot", Me.CurrentTarget, "ConcussiveShot");
                            SafelyFacingTarget(Me.CurrentTarget);
                            LastSnare = DateTime.Now;
                        })
                );
        }

        #endregion

        #region Cower

        private Composite Cower()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Cower &&
                Me.GotAlivePet &&
                Me.Pet.HealthPercent <= THSettings.Instance.CowerPercent &&
                Me.Pet.Combat &&
                !Me.Mounted &&
                PetCanCastAction("Cower"),
                new Action(
                    ret =>
                        {
                            PetCastAction("Cower", Me.Pet);
                            //Lua.DoString("RunMacroText('/Cast Cower');");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region DeterrenceHP

        //private Composite DeterrenceHP()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.DeterrenceHP &&
        //        SpellManager.HasSpell("Deterrence") &&
        //        !Me.Mounted &&
        //        Me.Combat &&
        //        Me.HealthPercent <= THSettings.Instance.DeterrenceHPPercent &&
        //        SpellManager.CanCast("Deterrence"),
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Deterrence", Me, "DeterrenceHP");
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        private static void DeterrenceHPVoid()
        {
            if (THSettings.Instance.DeterrenceHP &&
                SpellManager.HasSpell("Deterrence") &&
                !Me.Mounted &&
                Me.Combat &&
                !Me.HasAura("Ring of Peace") &&
                Me.HealthPercent <= THSettings.Instance.DeterrenceHPPercent &&
                !Me.HasAura("Deterrence") &&
                SpellManager.CanCast("Deterrence"))
            {
                CastSpell("Deterrence", Me, "DeterrenceHP");
            }
        }

        #endregion

        #region DeterrenceUnit

        //private Composite DeterrenceUnit()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.DeterrenceUnit &&
        //        SpellManager.HasSpell("Deterrence") &&
        //        !Me.Mounted &&
        //        Me.Combat &&
        //        CountMeleeDPSTarget(Me) >= THSettings.Instance.DeterrenceUnitNumber &&
        //        SpellManager.CanCast("Deterrence"),
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Deterrence", Me, "DeterrenceUnit");
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        private static void DeterrenceUnitVoid()
        {
            if (THSettings.Instance.DeterrenceUnit &&
                SpellManager.HasSpell("Deterrence") &&
                !Me.Mounted &&
                Me.Combat &&
                CountMeleeDPSTarget(Me) >= THSettings.Instance.DeterrenceUnitNumber &&
                !Me.HasAura("Deterrence") &&
                SpellManager.CanCast("Deterrence"))
            {
                CastSpell("Deterrence", Me, "DeterrenceUnit");
            }
        }

        #endregion

        #region Dire Beast

        private Composite DireBeast()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.DireBeast &&
                SpellManager.HasSpell("Dire Beast") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                //(target.IsPlayer ||
                // target.CurrentHealth > Me.MaxHealth*0.5 ||
                // IsDummy(target)) &&
                SpellManager.CanCast("Dire Beast"),
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            CastSpell("Dire Beast", Me.CurrentTarget, "DireBeast");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region Disengage

        private static int CountMeleeDisengage()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    return NearbyUnFriendlyUnits.Count(
                        unit =>
                        unit.IsValid &&
                        unit.IsPlayer &&
                        unit.CurrentTarget != null &&
                        unit.CurrentTarget.IsValid &&
                        unit.CurrentTarget == Me &&
                        Me.IsFacing(unit) &&
                        unit.Distance < 8 &&
                        TalentSort(unit) == 1 &&
                        !InvulnerableRootandSnare(unit) &&
                        !DebuffCC(unit));
                }
                return NearbyUnFriendlyUnits.Count(
                    unit =>
                    unit.IsValid &&
                    unit.CurrentTarget != null &&
                    unit.CurrentTarget.IsValid &&
                    unit.CurrentTarget == Me &&
                    Me.IsFacing(unit) &&
                    unit.Distance < 8 &&
                    unit.PowerType != WoWPowerType.Mana &&
                    !DebuffCC(unit));
            }
        }


        //private Composite Disengage()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.Disengage &&
        //        SpellManager.HasSpell("Disengage") &&
        //        !Me.Mounted &&
        //        Me.Combat &&
        //        CountMeleeDisengage() >= THSettings.Instance.DisengageUnitNumber &&
        //        SpellManager.CanCast("Disengage"),
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Disengage", Me.CurrentTarget, "Disengage");
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        private static void DisengageVoid()
        {
            if (THSettings.Instance.Disengage &&
                LastCastontheGround + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Disengage") &&
                !Me.Mounted &&
                Me.Combat &&
                !Me.HasAura("Death Grip") &&
                (!HasGlyph.Contains("119403") || //[Glyph of Explosive Trap - 119403] 
                 HasGlyph.Contains("119403") && !GetMyTrap(164839, Me, 10)) &&
                CountMeleeDisengage() >= THSettings.Instance.DisengageUnitNumber &&
                SpellManager.CanCast("Disengage"))
            {
                //if (!Me.IsFalling)
                //{
                WoWMovement.Move(WoWMovement.MovementDirection.JumpAscend);
                //}
                CastSpell("Disengage", Me, "Disengage");
            }
        }

        #endregion

        #region Exhilaration

        //private Composite Exhilaration()
        //{
        //    return new Decorator(
        //        ret =>
        //        SpellManager.HasSpell("Exhilaration") &&
        //        !Me.Mounted &&
        //        Me.Combat &&
        //        (THSettings.Instance.Exhilaration &&
        //         Me.HealthPercent <= THSettings.Instance.ExhilarationMe ||
        //         THSettings.Instance.ExhilarationPetHP &&
        //         Me.GotAlivePet &&
        //         Me.Pet.HealthPercent <= THSettings.Instance.ExhilarationPet) &&
        //        SpellManager.CanCast("Exhilaration"),
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Exhilaration", Me, "Exhilaration");
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        private static void ExhilarationVoid()
        {
            if (SpellManager.HasSpell("Exhilaration") &&
                !Me.Mounted &&
                Me.Combat &&
                (THSettings.Instance.Exhilaration &&
                 Me.HealthPercent <= THSettings.Instance.ExhilarationMe ||
                 THSettings.Instance.ExhilarationPetHP &&
                 Me.GotAlivePet &&
                 Me.Pet.HealthPercent <= THSettings.Instance.ExhilarationPet) &&
                SpellManager.CanCast("Exhilaration"))
            {
                CastSpell("Exhilaration", Me, "Exhilaration");
            }
        }

        #endregion

        #region Explosive Shot

        private Composite ExplosiveShot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ExplosiveShot &&
                SpellManager.HasSpell("Explosive Shot") &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 40) &&
                SpellManager.CanCast("Explosive Shot"),
                //(PlayerFocus >= 25 ||
                // LastCastSpell == "Cobra Shot" ||
                // Me.HasAura(56453)) &&
                //GetSpellCooldown("Explosive Shot") <= 1,
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            CastSpell("Explosive Shot", Me.CurrentTarget, "ExplosiveShot");
                            SafelyFacingTarget(Me.CurrentTarget);
                        })
                )
                ;
        }

        #endregion

        #region ExplosiveShotProc

        //private Composite ExplosiveShotProc()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.ExplosiveShot &&
        //        SpellManager.HasSpell("Explosive Shot") &&
        //        !Me.Mounted &&
        //        Attackable(Me.CurrentTarget, 40) &&
        //        Me.HasAura(56453) &&
        //        SpellManager.CanCast("Explosive Shot"),
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Explosive Shot", Me.CurrentTarget, "ExplosiveShotProc");
        //                    SafelyFacingTarget(Me.CurrentTarget);
        //                })
        //        );
        //}

        #endregion

        #region ExplosiveShotAoE

        //private static WoWUnit UnitExplosiveShot;

        //private static bool GetUnitExplosiveShot()
        //{
        //    UnitExplosiveShot = null;

        //    UnitExplosiveShot = (from unit in NearbyUnFriendlyUnits
        //                         where unit.IsValid
        //                         where unit.Combat || IsDummy(unit)
        //                         where
        //                             unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
        //                             unit.IsTargetingMeOrPet || IsDummy(unit)
        //                         where Me.IsFacing(unit)
        //                         where Attackable(unit, 40)
        //                         orderby MyAura(53301, unit) ascending
        //                         orderby unit.HealthPercent descending
        //                         select unit).FirstOrDefault();

        //    if (UnitExplosiveShot == null &&
        //        Attackable(Me.CurrentTarget, 40))
        //    {
        //        UnitExplosiveShot = Me.CurrentTarget;
        //    }
        //    return UnitExplosiveShot != null;
        //}

        //private Composite ExplosiveShotAoE()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.ExplosiveShot &&
        //        SpellManager.HasSpell("Explosive Shot") &&
        //        !Me.Mounted &&
        //        (PlayerFocus >= 25 ||
        //         Me.HasAura(56453)) &&
        //        GetSpellCooldown("Explosive Shot") <= 1 &&
        //        GetUnitExplosiveShot(),
        //        new Action(
        //            ret =>
        //                {
        //                    SafelyFacingTarget(UnitExplosiveShot);
        //                    CastSpell("Explosive Shot", UnitExplosiveShot, "ExplosiveShotAoE");
        //                })
        //        );
        //}

        private Composite ExplosiveShotAoEProc()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ExplosiveShot &&
                SpellManager.HasSpell("Explosive Shot") &&
                !Me.Mounted &&
                Me.GetAuraById(56453) != null &&
                Attackable(Me.CurrentTarget, 40) &&
                SpellManager.CanCast("Explosive Shot"),
                new Action(
                    ret =>
                        {
                            //Logging.Write("StackCount: " + Me.GetAuraById(56453).StackCount);
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Explosive Shot", Me.CurrentTarget, "ExplosiveShotAoEProc");
                            if (Me.GetAuraById(56453) != null &&
                                Me.GetAuraById(56453).StackCount == 1)
                            {
                                while (SpellManager.GlobalCooldown)
                                {
                                }
                            }
                        })
                );
        }

        #endregion

        #region ExplosiveTrapAoE

        private static WoWUnit UnitExplosiveTrap;

        private static bool GetUnitExplosiveTrap()
        {
            UnitExplosiveTrap = null;

            UnitExplosiveTrap = (from unit in NearbyUnFriendlyUnits
                                 where unit.IsValid
                                 where !unit.IsPet
                                 where unit.Combat || IsDummy(unit)
                                 where
                                     unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                     unit.IsTargetingMeOrPet || IsDummy(unit)
                                 where Me.IsFacing(unit)
                                 where Attackable(unit, 40)
                                 where CountEnemyNear(unit, 10) >= THSettings.Instance.UnittoStartAoE
                                 orderby CountEnemyNear(unit, 10) descending
                                 select unit).FirstOrDefault();

            return UnitExplosiveTrap != null;
        }

        private Composite ExplosiveTrapAoE()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ExplosiveTrapSV &&
                LastCastontheGround + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Explosive Trap") &&
                !HasGlyph.Contains("119403") && //[Glyph of Explosive Trap - 119403]
                !Me.Mounted &&
                Me.Combat &&
                GetSpellCooldown("Explosive Trap", 1000) <= TimeSpan.FromMilliseconds(400) &&
                //SpellManager.CanCast("Explosive Trap") &&
                GetUnitExplosiveTrap(),
                new Action(
                    ret =>
                        {
                            //SafelyFacingTarget(UnitExplosiveTrap);
                            if (!Me.HasAura(77769)) //Trap Launcher
                            {
                                CastSpell("Trap Launcher", Me, "Trap Launcher: ExplosiveTrapAoE");
                            }

                            LastCastontheGround = DateTime.Now;
                            while (GetSpellCooldown("Explosive Trap") > TimeSpan.FromMilliseconds(1))
                            {
                            }
                            CastSpell("Explosive Trap", UnitExplosiveTrap, "ExplosiveTrapAoE");

                            while (LastCastontheGround + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                                   Me.CurrentPendingCursorSpell == null)
                            {
                                Logging.Write("Waiting for CurrentPendingCursorSpell");
                            }
                            ObjectManager.Update();
                            SpellManager.ClickRemoteLocation(CalculateDropLocation(UnitExplosiveTrap));
                        })
                );
        }

        #endregion

        #region ExplosiveTrapDPSCurrentTarget

        private Composite ExplosiveTrapDPSCurrentTarget()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ExplosiveTrapDPS &&
                LastCastontheGround + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Explosive Trap") &&
                !HasGlyph.Contains("119403") && //[Glyph of Explosive Trap - 119403]
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.5 ||
                 IsDummy(Me.CurrentTarget) ||
                 Me.CurrentTarget.IsPlayer) &&
                //SpellManager.CanCast("Explosive Trap") ,
                GetSpellCooldown("Explosive Trap", 1000) <= TimeSpan.FromMilliseconds(400),
                new Action(
                    ret =>
                        {
                            //Logging.Write("ExplosiveTrapDPSCurrentTarget");
                            if (!Me.HasAura(77769)) //Trap Launcher
                            {
                                CastSpell("Trap Launcher", Me, "Trap Launcher: ExplosiveTrapDPSCurrentTarget");
                            }

                            LastCastontheGround = DateTime.Now;
                            while (GetSpellCooldown("Explosive Trap") > TimeSpan.FromMilliseconds(1))
                            {
                            }
                            CastSpell("Explosive Trap", Me.CurrentTarget, "ExplosiveTrapDPSCurrentTarget");

                            while (LastCastontheGround + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                                   Me.CurrentPendingCursorSpell == null)
                            {
                                Logging.Write("Waiting for CurrentPendingCursorSpell");
                            }
                            ObjectManager.Update();
                            SpellManager.ClickRemoteLocation(CalculateDropLocation(Me.CurrentTarget));
                        })
                );
        }

        #endregion

        #region ExplosiveTrapDPSAoE

        private static WoWUnit UnitExplosiveTrapDPSAoE;

        private static bool GetUnitExplosiveTrapDPSAoE()
        {
            UnitExplosiveTrapDPSAoE = null;

            UnitExplosiveTrapDPSAoE = (from unit in NearbyUnFriendlyUnits
                                       where unit.IsValid
                                       where !unit.IsPet
                                       where unit.CurrentHealth > Me.MaxHealth*0.7
                                       where unit.Combat || IsDummy(unit)
                                       where
                                           unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                           unit.IsTargetingMeOrPet || IsDummy(unit)
                                       where Attackable(unit, 40)
                                       orderby CountEnemyNear(unit, 10) descending
                                       select unit).FirstOrDefault();

            return UnitExplosiveTrapDPSAoE != null;
        }

        private Composite ExplosiveTrapDPSAoE()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.ExplosiveTrapDPS &&
                LastCastontheGround + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Explosive Trap") &&
                !HasGlyph.Contains("119403") && //[Glyph of Explosive Trap - 119403]
                !Me.Mounted &&
                Me.Combat &&
                //SpellManager.CanCast("Explosive Trap") &&
                GetSpellCooldown("Explosive Trap", 1000) <= TimeSpan.FromMilliseconds(400) &&
                GetUnitExplosiveTrapDPSAoE(),
                new Action(
                    ret =>
                        {
                            LastCastontheGround = DateTime.Now;

                            if (!Me.HasAura(77769)) //Trap Launcher
                            {
                                CastSpell("Trap Launcher", Me, "Trap Launcher: ExplosiveTrapDPSAoE");
                            }
                            while (GetSpellCooldown("Explosive Trap") > TimeSpan.FromMilliseconds(1))
                            {
                            }
                            CastSpell("Explosive Trap", UnitExplosiveTrapDPSAoE, "ExplosiveTrapAoE");

                            while (LastCastontheGround + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                                   Me.CurrentPendingCursorSpell == null)
                            {
                                Logging.Write("Waiting for CurrentPendingCursorSpell");
                            }
                            ObjectManager.Update();
                            SpellManager.ClickRemoteLocation(CalculateDropLocation(UnitExplosiveTrapDPSAoE));
                        })
                );
        }

        #endregion

        #region FocusFreezingTrap

        private static DateTime LastFocusFreezingTrap;

        private Composite FocusFreezingTrap()
        {
            return new Action(delegate
                {
                    if (THSettings.Instance.FocusFreezingTrap > 0 &&
                        SpellManager.HasSpell("Freezing Trap") &&
                        GetAsyncKeyState(IndexToKeys(THSettings.Instance.FocusFreezingTrap)) < 0 &&
                        AttackableNoCC(Me.FocusedUnit, 40) &&
                        GetSpellCooldown("Freezing Trap", 1000) <= TimeSpan.FromMilliseconds(400))
                    {
                        Logging.Write("CC on Demand: FocusFreezingTrap");
                        SpellManager.StopCasting();
                        if (!Me.HasAura(77769)) //Trap Launcher
                        {
                            CastSpell("Trap Launcher", Me, "Trap Launcher: FocusFreezingTrap");
                        }

                        while (GetSpellCooldown("Freezing Trap") > TimeSpan.FromMilliseconds(1))
                        {
                            Logging.Write("CC on Demand: FocusFreezingTrap TreeRoot.Stop");
                        }
                        LastFocusFreezingTrap = DateTime.Now;
                        CastSpell("Freezing Trap", Me.FocusedUnit, "FocusFreezingTrap");

                        while (LastFocusFreezingTrap + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                               Me.CurrentPendingCursorSpell == null)
                        {
                            Logging.Write("Waiting for CurrentPendingCursorSpell");
                            return RunStatus.Running;
                        }
                        ObjectManager.Update();
                        SpellManager.ClickRemoteLocation(Me.FocusedUnit.Location);
                        return RunStatus.Success;
                    }
                    return RunStatus.Failure;
                });
        }

        #endregion

        #region FeignDeathVoid

        private static DateTime LastFeignDeath;

        private static void FeignDeathVoid()
        {
            if (THSettings.Instance.FeignDeath &&
                SpellManager.HasSpell("Feign Death") &&
                !Me.Mounted &&
                Me.Combat &&
                !Me.IsMoving &&
                !Me.HasAura("Feign Death") &&
                Me.HealthPercent <= THSettings.Instance.FeignDeathPercent &&
                SpellManager.CanCast("Feign Death"))
            {
                //WoWMovement.MoveStop();
                LastFeignDeath = DateTime.Now;
                CastSpell("Feign Death", Me, "FeignDeath");
            }

            if (LastFeignDeath + TimeSpan.FromMilliseconds(2000) > DateTime.Now &&
                Me.HasAura("Feign Death") &&
                (CountEnemyNear(Me, 25) <= 0 ||
                 Me.HealthPercent >= THSettings.Instance.DoNotHealAbove ||
                 CountEnemyNearTarget(Me) > 0))
            {
                Logging.Write("Cancelaura Feign Death");
                Lua.DoString("RunMacroText('/Cancelaura Feign Death');");
            }
        }

        #endregion

        #region Fervor

        private Composite Fervor()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Fervor &&
                SpellManager.HasSpell("Fervor") &&
                !Me.Mounted &&
                Me.Combat &&
                PlayerFocus <= THSettings.Instance.FervorFocus &&
                Attackable(Me.CurrentTarget, 45) &&
                (Me.CurrentTarget.IsPlayer ||
                 Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.5 ||
                 IsDummy(Me.CurrentTarget)) &&
                SpellManager.CanCast("Fervor"),
                new Action(
                    ret =>
                        {
                            CastSpell("Fervor", Me, "Fervor");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region Fetch

        private static WoWUnit UnitFetch;

        private static bool GetUnitFetch()
        {
            UnitFetch = null;

            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitFetch = ObjectManager.GetObjectsOfType<WoWUnit>(true, false).FirstOrDefault(
                    u =>
                    u.Lootable &&
                    u.Location.Distance(Me.Pet.Location) < 30 &&
                    u.Distance < 40);
            }

            return UnitFetch != null;
        }

        private Composite Fetch()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Fetch &&
                SpellManager.HasSpell("Fetch") &&
                !Me.Combat &&
                GetUnitFetch() &&
                SpellManager.CanCast("Fetch"),
                new Action(
                    ret => { CastSpell("Fetch", UnitFetch, "Fetch"); })
                );
        }

        #endregion

        #region FocusFire

        private Composite FocusFire()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.FocusFire &&
                SpellManager.HasSpell("Focus Fire") &&
                !Me.Mounted &&
                !Me.HasAura("Focus Fire") &&
                Me.GotAlivePet &&
                Me.Pet.HasAura("Frenzy") &&
                (Me.Pet.GetAuraByName("Frenzy").StackCount == 5 &&
                 Attackable(Me.CurrentTarget, 40) ||
                 Me.Pet.GetAuraByName("Frenzy").TimeLeft.TotalMilliseconds < 5000) &&
                !Me.HasAura("Bestial Wrath") &&
                SpellManager.CanCast("Focus Fire"),
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Focus Fire", Me, "FocusFire");
                        })
                );
        }

        #endregion

        #region FreezingTrap

        private static WoWUnit UnitFreezingTrap;
        //private static bool FreezingTrapODMSearch;
        private static DateTime SearchFreezingTrapTime;

        private static DateTime LastCastontheGround;
        private static DateTime HoldDPSCurrentTarget;


        #endregion

        #region GlaiveToss

        private Composite GlaiveToss()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.GlaiveToss &&
                SpellManager.HasSpell("Glaive Toss") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                CountEnemyNearNoCCBreakonDamge(Me.CurrentTarget, 8) <= 0 &&
                //(target.IsPlayer ||
                // target.CurrentHealth > Me.MaxHealth * 0.5 ||
                // IsDummy(target)) &&
                SpellManager.CanCast("Glaive Toss"),
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Glaive Toss", Me.CurrentTarget, "GlaiveToss");
                        })
                );
        }

        #endregion

        #region GlaiveTossSnare

        private Composite GlaiveTossSnare()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.GlaiveToss &&
                LastSnare + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                SpellManager.HasSpell("Glaive Toss") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                Me.CurrentTarget.IsPlayer &&
                //FacingOverrideWide(Me.CurrentTarget) &&
                !DebuffRootorSnare(Me.CurrentTarget) &&
                //(Me.CurrentTarget.IsPlayer ||
                // Me.CurrentTarget.CurrentHealth > Me.MaxHealth * 0.5 ||
                // IsDummy(Me.CurrentTarget)) &&
                SpellManager.CanCast("Glaive Toss"),
                new Action(
                    ret =>
                        {
                            CastSpell("Glaive Toss", Me.CurrentTarget, "GlaiveTossSnare");
                            SafelyFacingTarget(Me.CurrentTarget);
                            LastSnare = DateTime.Now;
                        })
                );
        }

        #endregion

        #region GetMyTrap

        private static WoWUnit MyTrap;

        private static bool GetMyTrap(int trapEntry, WoWUnit target, int distance)
        {
            //Snake Trap 183957
            //Freeezing Trap 2561
            //Explosive Trap 164839
            //Ice Trap 164639

            MyTrap = null;

            MyTrap =
                ObjectManager.GetObjectsOfType<WoWUnit>(true, false)
                             .FirstOrDefault(
                                 p =>
                                 p.CreatedByUnitGuid == Me.Guid &&
                                 p.Entry == trapEntry &&
                                 target.Location.Distance(p.Location) <= distance);

            return MyTrap != null;
        }

        #endregion

        #region Hold

        private Composite Hold()
        {
            return new Decorator(
                ret =>
                !Me.IsValid ||
                !StyxWoW.IsInWorld ||
                !Me.IsAlive ||
                Me.HasAura("Food") && Me.HealthPercent < THSettings.Instance.DoNotHealAbove ||
                !THSettings.Instance.AttackOOC && !Me.Combat ||
                Me.HasAura("Resurrection Sickness"),
                new Action(delegate
                    {
                        if (Me.IsAutoAttacking)
                        {
                            Lua.DoString("RunMacroText('/stopattack');");
                        }

                        Logging.Write("Hold on Aat/Drink/Dead/Not in Game/Sesurrection Sickness");
                        return RunStatus.Success;
                    })
                );
        }

        private Composite HoldDungeon()
        {
            return new Decorator(
                ret =>
                (Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                !Me.Combat && Me.GotAlivePet &&
                (Me.CurrentTarget == null ||
                 Me.CurrentTarget != null &&
                 IsEnemy(Me.CurrentTarget) &&
                 !Me.CurrentTarget.IsTargetingMyPartyMember &&
                 !Me.CurrentTarget.IsTargetingMyRaidMember &&
                 !Me.CurrentTarget.IsTargetingMeOrPet),
                new Action(delegate
                    {
                        if (Me.IsAutoAttacking)
                        {
                            Lua.DoString("RunMacroText('/stopattack');");
                        }

                        Logging.Write("Hold HoldDungeon");
                        return RunStatus.Success;
                    })
                );
        }

        private Composite HoldFeignDeath()
        {
            return new Decorator(
                ret =>
                Me.HasAura("Feign Death") &&
                Me.HealthPercent < THSettings.Instance.PriorityHeal ||
                LastFeignDeath + TimeSpan.FromMilliseconds(2000) > DateTime.Now,
                new Action(delegate
                    {
                        Logging.Write("HoldFeignDeath");
                        return RunStatus.Success;
                    })
                );
        }

        private static DateTime LastSpiritWalk;

        private Composite HoldCamouflage()
        {
            return
                new Decorator(
                    ret =>
                    Me.HasAura("Camouflage"),
                    //(!Me.IsMoving ||
                    // HasGlyph.Contains("119449")), //Glyph of Camouflage - 119449
                    new Action(delegate
                        {
                            Logging.Write("Hold Camouflage");
                            if (LastSpiritWalk + TimeSpan.FromSeconds(10) < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/cast Spirit Walk');");
                                LastSpiritWalk = DateTime.Now;
                            }
                            return RunStatus.Success;
                        })
                    )
                ;
        }

        #endregion

        #region Hunter's Mark

        //private Composite HuntersMark()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.HuntersMark &&
        //        SpellManager.HasSpell("Hunter's Mark") &&
        //        !Me.Mounted &&
        //        Attackable(Me.CurrentTarget, 100) &&
        //        Me.CurrentTarget.Distance > 40 &&
        //        (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.7 ||
        //         IsDummy(Me.CurrentTarget)) &&
        //        !Me.CurrentTarget.HasAura("Hunter's Mark") &&
        //        !InvulnerableSpell(Me.CurrentTarget) &&
        //        SpellManager.CanCast("Hunter's Mark"),
        //        new Action(
        //            ret =>
        //                {
        //                    SafelyFacingTarget(Me.CurrentTarget);
        //                    CastSpell("Hunter's Mark", Me.CurrentTarget, "HuntersMark");
        //                })
        //        );
        //}

        #endregion

        #region Hunter's Mark OOC

        //private static WoWUnit UnitHuntersMarkOOC;

        //private static bool GetUnitHuntersMarkOOC()
        //{
        //    //using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        Logging.Write("Scanning for Hunter's Mark Out of Combat");
        //        UnitHuntersMarkOOC = null;
        //        UnitHuntersMarkOOC = (from unit in NearbyUnFriendlyUnits
        //                              where unit.IsValid
        //                              where unit.Level >= Me.Level
        //                              where unit.IsPlayer || unit.Elite || unit.IsBoss
        //                              where Attackable(unit, 100)
        //                              where !unit.HasAura("Hunter's Mark")
        //                              orderby unit.CurrentHealth ascending
        //                              select unit).FirstOrDefault();
        //        return UnitHuntersMarkOOC != null;
        //    }
        //}

        //private Composite HuntersMarkOOC()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.HuntersMarkOOC &&
        //        SpellManager.HasSpell("Hunter's Mark") &&
        //        //!Me.HasAura("Camouflage") &&
        //        //Me.IsInInstance &&
        //        !Me.Combat &&
        //        !Me.Mounted &&
        //        !SpellManager.GlobalCooldown &&
        //        GetUnitHuntersMarkOOC(),
        //        new Action(
        //            ret =>
        //                {
        //                    //SafelyFacingTarget(Me.CurrentTarget);
        //                    CastSpell("Hunter's Mark", UnitHuntersMarkOOC, "HuntersMarkOOC");
        //                })
        //        );
        //}

        #endregion

        #region IceTrapAoE

        private static WoWUnit UnitExplosiveTrapAlready;

        private static bool GetUnitUnitExplosiveTrapAlready()
        {
            UnitExplosiveTrapAlready = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitExplosiveTrap = (from unit in NearbyUnFriendlyUnits
                                     where unit.IsValid
                                     where MyAura("Explosive Trap", unit)
                                     select unit).FirstOrDefault();
            }
            return UnitExplosiveTrapAlready != null;
        }

        private static WoWUnit UnitIceTrap;

        private static bool GetUnitIceTrap()
        {
            UnitIceTrap = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitIceTrap = (from unit in NearbyUnFriendlyUnits
                               where unit.IsValid
                               where unit.Combat || IsDummy(unit)
                               where !unit.IsPet
                               where unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                     unit.IsTargetingMeOrPet || IsDummy(unit)
                               where Me.IsFacing(unit)
                               where Attackable(unit, 40)
                               where !MyAura("Explosive Trap", unit)
                               where CountEnemyNear(unit, 10) >= THSettings.Instance.UnittoStartAoE
                               orderby CountEnemyNear(unit, 10) descending
                               select unit).FirstOrDefault();
            }
            return UnitIceTrap != null;
        }

        private Composite IceTrapAoE()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.IceTrapSV &&
                SpellManager.HasSpell("Ice Trap") &&
                LastCastontheGround + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                !Me.Mounted &&
                Me.Combat &&
                Me.GetAuraById(56453) == null && //"Lock and Load"
                //SpellManager.CanCast("Ice Trap") &&
                GetSpellCooldown("Ice Trap", 1000) <= TimeSpan.FromMilliseconds(400) &&
                !GetUnitUnitExplosiveTrapAlready() &&
                GetUnitIceTrap(),
                new Action(
                    ret =>
                        {
                            if (!Me.HasAura(77769)) //Trap Launcher
                            {
                                CastSpell("Trap Launcher", Me, "Trap Launcher: IceTrapAoE");
                            }

                            LastCastontheGround = DateTime.Now;
                            while (GetSpellCooldown("Ice Trap") > TimeSpan.FromMilliseconds(1))
                            {
                            }
                            CastSpell("Ice Trap", UnitIceTrap, "IceTrapAoE");

                            while (LastCastontheGround + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                                   Me.CurrentPendingCursorSpell == null)
                            {
                                Logging.Write("Waiting for CurrentPendingCursorSpell");
                            }
                            ObjectManager.Update();
                            SpellManager.ClickRemoteLocation(CalculateDropLocation(UnitIceTrap));
                        })
                );
        }

        #endregion

        #region IceTrapSV

        private Composite IceTrapSV()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.IceTrapSV &&
                LastCastontheGround + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Ice Trap") &&
                SpellManager.HasSpell("Explosive Trap") &&
                LastCastSpell != "Black Arrow" &&
                !Me.Mounted &&
                Me.Combat &&
                Me.GetAuraById(56453) == null &&
                Attackable(Me.CurrentTarget, 40) &&
                !MyAura("Black Arrow", Me.CurrentTarget) &&
                !MyAura("Explosive Trap", Me.CurrentTarget) &&
                //SpellManager.CanCast("Ice Trap") &&
                GetSpellCooldown("Explosive Trap") > TimeSpan.FromMilliseconds(5000) &&
                GetSpellCooldown("Ice Trap", 1000) <= TimeSpan.FromMilliseconds(400) &&
                !GetUnitUnitBlackArrow(), // &&
                //!Me.CurrentTarget.HasAura("Explosive Trap") &&
                //!Me.CurrentTarget.HasAura("Black Arrow"),
                new Action(
                    ret =>
                        {
                            if (!Me.HasAura(77769)) //Trap Launcher
                            {
                                CastSpell("Trap Launcher", Me, "Trap Launcher: IceTrapSV");
                            }

                            LastCastontheGround = DateTime.Now;
                            while (GetSpellCooldown("Ice Trap") > TimeSpan.FromMilliseconds(1))
                            {
                            }
                            CastSpell("Ice Trap", Me.CurrentTarget, "IceTrapSV");

                            while (LastCastontheGround + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                                   Me.CurrentPendingCursorSpell == null)
                            {
                                Logging.Write("Waiting for CurrentPendingCursorSpell");
                            }
                            ObjectManager.Update();
                            SpellManager.ClickRemoteLocation(CalculateDropLocation(Me.CurrentTarget));
                        })
                )
                ;
        }

        #endregion

        #region IntimidationInterrupt

        private static DateTime DoNotFreezingTrap;
        
        #endregion

        #region IntimidationLowHP

        private static WoWUnit UnitIntimidationLowHP;

        private static bool GetUnitIntimidationLowHP()
        {
            UnitIntimidationLowHP = null;

            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.GotAlivePet &&
                    Me.Pet.CurrentTarget != null &&
                    Me.Pet.Location.Distance(Me.Pet.CurrentTarget.Location) < 10 &&
                    IsEnemy(Me.Pet.CurrentTarget) &&
                    (Me.Pet.CurrentTarget.HealthPercent < THSettings.Instance.IntimidationLowHPPercent ||
                     Me.Minions.Count > 3 &&
                     Me.CurrentTarget != null &&
                     Me.Pet.CurrentTarget == Me.CurrentTarget) && //Always stun in Stampede
                    Attackable(Me.Pet.CurrentTarget) &&
                    !InvulnerableStun(Me.Pet.CurrentTarget))
                {
                    UnitIntimidationLowHP = Me.Pet.CurrentTarget;
                }
            }
            return UnitIntimidationLowHP != null;
        }

        private static DateTime PetStunLast;

        #endregion

        #region Kill Command

        private Composite KillCommand()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.KillCommand &&
                LastPetRangAttack + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                HoldPetAction + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Kill Command") &&
                Me.GotAlivePet &&
                !Me.Mounted &&
                PlayerFocus >= 40 &&
                AttackableNoLoS(Me.CurrentTarget, 60) &&
                SpellManager.CanCast("Kill Command"),
                new Action(
                    ret =>
                        {
                            LastPetRangAttack = DateTime.Now;
                            Lua.DoString("RunMacroText('/Cast Rabid');");
                            if (Me.Pet.CurrentTarget == null ||
                                Me.Pet.CurrentTarget != null && Me.Pet.CurrentTarget != Me.CurrentTarget)
                            {
                                Lua.DoString("RunMacroText('/PetAttack');");
                            }

                            //if (GetSpellCooldown("Kill Command") > TimeSpan.FromMilliseconds(400) ||
                            //    Me.IsCasting && Me.CurrentCastTimeLeft > TimeSpan.FromMilliseconds(400) ||
                            //    GetSpellCooldown("Mend Pet") > TimeSpan.FromMilliseconds(400))
                            //{
                            //    Logging.Write("Waiting for Kill Command return");
                            //    return RunStatus.Running;
                            //}

                            CastSpell("Kill Command", Me.CurrentTarget, "KillCommand");
                        })
                )
                ;
        }

        private Composite KillCommandCamouflage()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.KillCommand &&
                THSettings.Instance.Camouflage &&
                Me.HasAura("Camouflage") &&
                THSettings.Instance.KillCommand &&
                SpellManager.HasSpell("Kill Command") &&
                HoldPetAction + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                Me.GotAlivePet &&
                !Me.Mounted &&
                PlayerFocus >= 40 &&
                AttackableNoLoS(Me.CurrentTarget, 60) &&
                SpellManager.CanCast("Kill Command"),
                new Action(
                    ret => { CastSpell("Kill Command", Me.CurrentTarget, "KillCommandCamouflage"); })
                )
                ;
        }

        #endregion

        #region Kill Shot CurrentTarget

        private static DateTime LastKillShot;

        private Composite KillShotCurrentTarget()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.KillShot &&
                //LastKillShot + TimeSpan.FromMilliseconds(1200) < DateTime.Now &&
                SpellManager.HasSpell("Kill Shot") &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 45) &&
                Me.CurrentTarget.HealthPercent <= 20 &&
                FacingOverride(Me.CurrentTarget) &&
                GetSpellCooldown("Kill Shot") <= TimeSpan.FromMilliseconds(800),
                //SpellManager.CanCast("Kill Shot", Me.CurrentTarget),
                new Action(
                    ret =>
                        {
                            LastKillShot = DateTime.Now;
                            SpellManager.StopCasting();
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            SafelyFacingTarget(Me.CurrentTarget);
                            while (LastKillShot + TimeSpan.FromMilliseconds(800) > DateTime.Now &&
                                   GetSpellCooldown("Kill Shot") <= TimeSpan.FromMilliseconds(800))
                            {
                                CastSpell("Kill Shot", Me.CurrentTarget, "KillShotCurrentTarget");
                            }
                        })
                );
        }

        #endregion

        #region Last Stand

        private Composite LastStand()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.LastStand &&
                Me.GotAlivePet &&
                Me.Pet.Combat &&
                Me.Pet.HealthPercent <= THSettings.Instance.LastStandPercent &&
                PetCanCastAction("Last Stand"),
                new Action(
                    ret =>
                        {
                            PetCastAction("Last Stand", Me.Pet);
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region Lynx Rush

        private Composite LynxRush()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.LynxRush &&
                (THSettings.Instance.AMoCLRCD ||
                 THSettings.Instance.AMoCLRCBurst &&
                 Burst) &&
                SpellManager.HasSpell("Lynx Rush") &&
                !Me.Mounted &&
                Me.Combat &&
                Me.GotAlivePet &&
                Attackable(Me.CurrentTarget, 45) &&
                //Me.Pet.Location.Distance(target.Location) < 10 &&
                (Me.CurrentTarget.IsPlayer ||
                 Me.CurrentTarget.CurrentHealth >
                 Me.MaxHealth*0.5 ||
                 IsDummy(Me.CurrentTarget) ||
                 CountEnemyNear(Me.CurrentTarget, 10) > THSettings.Instance.UnittoStartAoE) &&
                SpellManager.CanCast("Lynx Rush"),
                new Action(
                    ret =>
                        {
                            CastSpell("Lynx Rush", Me.CurrentTarget, "LynxRush");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region Mend Pet

        private Composite MendPet()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.MendPet &&
                SpellManager.HasSpell("Mend Pet") &&
                Me.Level >= 16 && //Low level
                Me.GotAlivePet &&
                !Me.Pet.HasAura("Mend Pet") &&
                (HasGlyph.Contains("19573") && //Glyph of Mend Pet - 19573
                 Me.Pet.Debuffs.Count > 0 ||
                 Me.Pet.HealthPercent <= THSettings.Instance.MendPetPercent ||
                 !Me.Combat &&
                 Me.Pet.HealthPercent <= THSettings.Instance.DoNotHealAbove) &&
                Me.Pet.Distance < 35 &&
                (!HasGlyph.Contains("19573") ||
                 !DebuffDoNotCleanse(Me.Pet)) &&
                Me.Pet.InLineOfSight,
                new Action(
                    ret => CastSpell("Mend Pet", Me.Pet, "MendPet"))
                );
        }

        #endregion

        #region MasterCallMe

        private static int CountMeleeMasterCallMe()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                return NearbyUnFriendlyUnits.Count(
                    unit =>
                    unit.IsValid &&
                    unit.IsPlayer &&
                    unit.CurrentTarget != null &&
                    unit.CurrentTarget.IsValid &&
                    unit.CurrentTarget == Me &&
                    Attackable(unit, 8) &&
                    TalentSort(unit) == 1 &&
                    (!DebuffRoot(unit) || DebuffRoot(Me)));
            }
        }

        private Composite MasterCallMe()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.MasterCallMe &&
                LastCastontheGround + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                SpellManager.HasSpell("Master's Call") &&
                LastCastSpell != "Disengage" &&
                !Me.Mounted &&
                Me.Combat &&
                Me.GotAlivePet &&
                (DebuffRoot(Me) &&
                 DebuffRoot(Me.Pet) ||
                 DebuffRootorSnare(Me) &&
                 CountMeleeMasterCallMe() > 0) &&
                SpellManager.CanCast("Master's Call"),
                new Action(
                    ret =>
                        {
                            if (Me.Pet.Distance > 40 ||
                                !Me.Pet.InLineOfSpellSight)
                            {
                                HoldPetAction = DateTime.Now;
                                Logging.Write("PetFollow MasterCallMe");
                                Lua.DoString("RunMacroText('/PetPassive');");
                                Lua.DoString("RunMacroText('/PetFollow');");
                            }
                            else
                            {
                                CastSpell("Master's Call", Me, "MasterCallMe");
                            }
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region MisdirectionPet

        private Composite MisdirectionPet()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Misdirection &&
                SpellManager.HasSpell("Misdirection") &&
                !Me.Mounted &&
                !Me.IsInInstance &&
                Me.GotAlivePet &&
                Attackable(Me.CurrentTarget, 100) &&
                !Me.CurrentTarget.IsPlayer &&
                !Me.CurrentTarget.IsPet &&
                !Me.HasAura("Misdirection") &&
                Me.Pet.Distance < 100 &&
                Me.Pet.InLineOfSpellSight &&
                //(!Me.Combat && Me.CurrentTarget != null || GetUnitAttackingMe()) &&
                SpellManager.CanCast("Misdirection"),
                new Action(
                    ret =>
                        {
                            CastSpell("Misdirection", Me.Pet, "MisdirectionPet");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region MisdirectionTank

        private Composite MisdirectionTank()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Misdirection &&
                SpellManager.HasSpell("Misdirection") &&
                !Me.Mounted &&
                (Me.CurrentMap.IsDungeon ||
                 Me.CurrentMap.IsRaid) &&
                Attackable(Me.CurrentTarget, 40) &&
                //!Me.CurrentTarget.IsPlayer &&
                //!Me.CurrentTarget.IsPet &&
                Me.CurrentTarget.CurrentTarget != null &&
                Me.CurrentTarget.CurrentTarget.HasAura("Vengeance") &&
                SpellManager.CanCast("Misdirection"),
                new Action(
                    ret =>
                        {
                            CastSpell("Misdirection", Me.CurrentTarget.CurrentTarget, "MisdirectionTank");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region MultiShot

        private Composite MultiShot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.MultiShot &&
                SpellManager.HasSpell("Multi-Shot") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Multi-Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Multi-Shot", Me.CurrentTarget, "MultiShot");
                        })
                );
        }

        #endregion

        #region PetAutocastOff

        private static void PetAutocastOff()
        {
            if (Me.GotAlivePet)
            {
                Logging.Write("Toggle Few Imporant Spells Pet Autocast Off");
                Logging.Write("Better let Combat Routine handle it");
                Lua.DoString("RunMacroText('/petautocastoff Lullaby');");
                Lua.DoString("RunMacroText('/petautocastoff Web Wrap');");
                Lua.DoString("RunMacroText('/petautocastoff Bad Manner');");
                Lua.DoString("RunMacroText('/petautocastoff Growl');");
                Lua.DoString("RunMacroText('/petautocastoff Cower');");
                Lua.DoString("RunMacroText('/petautocastoff Spirit Mend');");
                Lua.DoString("RunMacroText('/petautocastoff Rabid');");
                Lua.DoString("RunMacroText('/petautocastoff Roar of Sacrifice');");
                Lua.DoString("RunMacroText('/petautocastoff Bullheaded');");
            }
        }

        #endregion

        #region PetCastAction

        private static void PetCastAction(string action, WoWUnit on)
        {
            WoWUnit OriginalFocus;
            OriginalFocus = null;
            WoWPetSpell spell = PetSpells.FirstOrDefault(p => p.ToString() == action);
            if (spell == null)
            {
                Logging.Write("Your pet don't have " + action);
                return;
            }

            //if (action != "Attack")
            //{
            //    Logging.Write("[Pet] Casting {0} on {1}", action, SafeName(on));
            //}

            if (Me.CurrentTarget != null && on == Me.CurrentTarget)
            {
                Lua.DoString("CastPetAction({0}, 'target')", spell.ActionBarIndex + 1);
            }
            else if (Me.CurrentTarget != null && on == Me)
            {
                Lua.DoString("CastPetAction({0}, 'player')", spell.ActionBarIndex + 1);
            }
            else if (Me.CurrentTarget != null && on == Me.Pet)
            {
                Lua.DoString("CastPetAction({0}, 'pet')", spell.ActionBarIndex + 1);
            }
            else
            {
                if (Me.FocusedUnit != null)
                {
                    OriginalFocus = Me.FocusedUnit;
                }

                StyxWoW.Me.SetFocus(on);
                Lua.DoString("CastPetAction({0}, 'focus')", spell.ActionBarIndex + 1);
                if (OriginalFocus != null)
                {
                    StyxWoW.Me.SetFocus(OriginalFocus);
                }
                else
                {
                    StyxWoW.Me.SetFocus(0);
                }
            }
        }

        #endregion

        #region PetCanCastAction

        private static bool PetCanCastAction(string action)
        {
            WoWPetSpell petAction = PetSpells.FirstOrDefault(p => p.ToString() == action);
            if (petAction == null || petAction.Spell == null)
            {
                return false;
            }

            return !petAction.Spell.Cooldown;
        }

        #endregion

        #region PetAttack

        private static DateTime HoldPetAction;

        private Composite PetAttack()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.PetAttack &&
                HoldPetAction + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                //!PetFollowMode &&
                Me.GotAlivePet &&
                !Me.Pet.HasAura("Intimidation") &&
                (Me.Pet.HealthPercent >= THSettings.Instance.PetAttackHP ||
                 Me.Pet.Distance < 10) &&
                AttackableNoLoS(Me.CurrentTarget, 60) &&
                (Me.Pet.CurrentTarget == null ||
                 !Me.CurrentMap.IsArena &&
                 Me.Pet.CurrentTarget != null &&
                 Me.CurrentTarget != null &&
                 Me.Pet.CurrentTarget != Me.CurrentTarget ||
                 Me.CurrentMap.IsArena &&
                 Me.Pet.CurrentTarget != null &&
                 Me.CurrentTarget != null &&
                 Me.Pet.CurrentTarget != Me.CurrentTarget &&
                 !Me.Pet.CurrentTarget.IsPlayer),
                new Action(
                    ret =>
                        {
                            Logging.Write("PetAttack");
                            if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                            {
                                Lua.DoString("RunMacroText('/PetPassive');");
                            }
                            else
                            {
                                Lua.DoString("RunMacroText('/PetAssist');");
                            }
                            Lua.DoString("RunMacroText('/PetAttack');");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region PetAttackUnitAggroMe

        private static WoWUnit UnitAttackingMe;

        private static bool GetUnitAttackingMe()
        {
            UnitAttackingMe = null;
            if (Me.CurrentMap.IsBattleground ||
                Me.CurrentMap.IsArena)
            {
                return false;
            }
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitAttackingMe = (from unit in NearbyUnFriendlyUnits
                                   where unit.IsValid
                                   where
                                       unit.ThreatInfo.RawPercent > 50 ||
                                       unit.CurrentTarget != null && unit.CurrentTarget == Me
                                   //orderby unit.HealthPercent ascending
                                   orderby unit.ThreatInfo.RawPercent descending
                                   where unit.Combat
                                   where !unit.IsPet
                                   where !unit.IsPlayer
                                   where unit.IsTargetingMeOrPet
                                   //where unit.CurrentTarget == Me
                                   select unit).FirstOrDefault();
                //if (UnitAttackingMe != null)
                //{
                //    Logging.Write("Found UnitAttackingMe");
                //}
                //else
                //{
                //    Logging.Write("Not Found UnitAttackingMe");
                //}
            }
            return UnitAttackingMe != null;
        }

        private Composite PetTankAdd()
        {
            return new PrioritySelector(
                MisdirectionPet(),
                new Decorator(
                    ret =>
                    THSettings.Instance.PetAttack &&
                    THSettings.Instance.PetTankAdd &&
                    HoldPetAction + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                    //Me.Aggro &&
                    Me.GotAlivePet &&
                    Me.Pet.HealthPercent >= THSettings.Instance.PetFollowHP &&
                    GetUnitAttackingMe(), // &&
                    //(Me.Pet.CurrentTarget == null ||
                    // Me.Pet.CurrentTarget != null &&
                    // Me.Pet.CurrentTarget != UnitAttackingMe),
                    new Action(
                        ret =>
                            {
                                Logging.Write("PetAttackUnitAggroMe");
                                Lua.DoString("RunMacroText('/PetDefensive');");
                                HoldPetAction = DateTime.Now + TimeSpan.FromMilliseconds(3000);
                                if ((Me.Pet.CurrentTarget == null ||
                                     Me.Pet.CurrentTarget != null &&
                                     Me.Pet.CurrentTarget != UnitAttackingMe) && Me.CurrentTarget != null)
                                {
                                    UnitAttackingMe.Target();
                                    Lua.DoString("RunMacroText('/PetAttack');");
                                    Me.TargetLastTarget();
                                }

                                if (THSettings.Instance.PetTankAdd &&
                                    PetCanCastAction("Growl"))
                                {
                                    PetCastAction("Growl", UnitAttackingMe);
                                }
                                return RunStatus.Failure;
                            })
                    ));
        }

        #endregion

        #region PetFocus

        //private static double PetFocus
        //{
        //    get
        //    {
        //        try
        //        {
        //            //using (StyxWoW.Memory.AcquireFrame())
        //            {
        //                return Lua.GetReturnVal<int>("return UnitPower(\"pet\", SPELL_POWER_FOCUS);", 0);
        //            }
        //        }
        //        catch
        //        {
        //            Logging.Write(" Lua Failed in PetFocus");
        //            return 0;
        //        }
        //    }
        //}

        #endregion

        #region PetFollow

        private Composite PetFollow()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.PetFollow &&
                HoldPetAction + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
                Me.GotAlivePet &&
                (Me.Pet.HealthPercent <= THSettings.Instance.PetFollowHP ||
                 THSettings.Instance.PetFollowDist && Me.Pet.Distance > THSettings.Instance.PetFollowDistYard) &&
                Me.Pet.CurrentTarget != null,
                new Action(
                    ret =>
                        {
                            //PetFollowMode = true;
                            HoldPetAction = DateTime.Now;
                            Logging.Write("PetFollow");
                            Lua.DoString("RunMacroText('/PetPassive');");
                            Lua.DoString("RunMacroText('/PetFollow');");
                            if (Me.Pet.HealthPercent <= THSettings.Instance.UrgentHeal)
                            {
                                Lua.DoString("RunMacroText('/cast [@pet] Dash');");
                            }
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region SetPetFollowMode

        //private static bool PetFollowMode = true;

        //private Composite SetPetFollowMode()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.PetAttack &&
        //        HoldPetAction + TimeSpan.FromMilliseconds(3000) > DateTime.Now &&
        //        Me.GotAlivePet &&
        //        Me.Pet.HealthPercent >= THSettings.Instance.PetAttackHP &&
        //        Me.Pet.Distance <= 25 &&
        //        Me.Pet.InLineOfSpellSight6666,
        //        new Action(
        //            ret =>
        //                {
        //                    //PetFollowMode = false;
        //                    //Logging.Write("SetPetFollowMode = false");
        //                    Lua.DoString("RunMacroText('/PetAssit');");
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        #endregion

        #region PetSpellsUpdate

        private static readonly List<WoWPetSpell> PetSpells = new List<WoWPetSpell>();

        private static void PetSpellsUpdate()
        {
            if (Me.GotAlivePet)
            {
                Logging.Write("PetSpellsUpdate");
                Logging.Write("Toggle Pet Autocast Off some important spells.");
                Logging.Write("Better let Combat Routine handle it.");
                Lua.DoString("RunMacroText('/petautocastoff Growl');");
                Lua.DoString("RunMacroText('/petautocastoff Cower');");
                Lua.DoString("RunMacroText('/petautocastoff Spirit Mend');");
                Lua.DoString("RunMacroText('/petautocastoff Roar of Sacrifice');");
                Lua.DoString("RunMacroText('/petautocastoff Bullheaded');");

                PetSpells.Clear();
                PetSpells.AddRange(Me.PetSpells);
                Logging.Write("Your Current Pet Action Bar:");
                foreach (WoWPetSpell petSpell in PetSpells)
                {
                    if (petSpell.Action.ToString() != "None")
                    {
                        Logging.Write(petSpell.Action.ToString());
                    }
                    else if (petSpell.Spell != null)
                    {
                        Logging.Write(petSpell.Spell.ToString());
                    }
                    else
                    {
                        Logging.Write(petSpell.Stance.ToString());
                    }
                }
                Logging.Write(
                    "Notice: To Make Combat Routine Use a Pet Ability, You Need to Drag it into the Pet's Action Bar!");
            }
        }

        #endregion

        #region PetGetSpellCooldown

        private static TimeSpan PetGetSpellCooldown(string spellname)
        {
            WoWPetSpell spell = PetSpells.FirstOrDefault(p => p.ToString() == spellname);
            if (spell == null)
            {
                return TimeSpan.FromMilliseconds(0);
            }
            return spell.Spell.CooldownTimeLeft;
        }

        #endregion

        #region PetHasSpell

        private static bool PetHasSpell(string spellname)
        {
            if (!Me.GotAlivePet)
            {
                return false;
            }
            WoWPetSpell spell = PetSpells.FirstOrDefault(p => p.ToString() == spellname);
            return spell != null;
        }

        #endregion

        #region PlayerFocus

        //private static double PlayerFocus
        //{
        //    get
        //    {
        //        try
        //        {
        //            //using (StyxWoW.Memory.AcquireFrame())
        //            {
        //                return Lua.GetReturnVal<int>("return UnitPower(\"player\", SPELL_POWER_FOCUS);", 0);
        //            }
        //        }
        //        catch
        //        {
        //            Logging.Write(" Lua Failed in PlayerFocus");
        //            return 0;
        //        }
        //    }
        //}

        //private static double PlayerFocus
        //{
        //    get
        //    {
        //        if (Me.IsCasting)
        //        {
        //            if (Me.CastingSpell.Name == "Steady Shot" || Me.CastingSpell.Name == "Cobra Shot")
        //            {
        //                return Me.CurrentFocus + 25;
        //            }

        //            if (Me.CastingSpell.Name == "Aimed Shot")
        //            {
        //                return Me.CurrentFocus - 40;
        //            }
        //        }
        //        return Me.CurrentFocus;
        //    }
        //}

        private static double PlayerFocus
        {
            get
            {
                try
                {
                    using (StyxWoW.Memory.AcquireFrame())
                    {
                        double currentFocus = Lua.GetReturnVal<int>("return UnitPower(\"player\", SPELL_POWER_FOCUS);",
                                                                    0);
                        if (Me.IsCasting)
                        {
                            if (Me.CastingSpell.Name == "Steady Shot" || Me.CastingSpell.Name == "Cobra Shot")
                            {
                                return currentFocus + 17;
                            }

                            if (Me.CastingSpell.Name == "Aimed Shot")
                            {
                                return currentFocus - 40;
                            }
                        }
                        return currentFocus;
                    }
                }
                catch
                {
                    Logging.Write(" Lua Failed in SPELL_POWER_FOCUS");
                    return 0;
                }
            }
        }

        #endregion

        #region Powershot

        private Composite Powershot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Powershot &&
                SpellManager.HasSpell("Powershot") &&
                !Me.Mounted &&
                Me.Combat &&
                !Me.IsMoving &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                //(target.IsPlayer ||
                // target.CurrentHealth > Me.MaxHealth * 0.5 ||
                // IsDummy(target)) &&
                SpellManager.CanCast("Powershot"),
                new Action(
                    ret =>
                        {
                            if (HoldPetAction < DateTime.Now)
                            {
                                Lua.DoString("RunMacroText('/Cast Rabid');");
                            }
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Powershot", Me.CurrentTarget, "Powershot");
                        })
                );
        }

        #endregion

        #region PredictFocus

        //private static double PlayerFocus
        //{
        //    if (Me.IsCasting)
        //    {
        //        if (Me.CastingSpell.Name == "Steady Shot" || Me.CastingSpell.Name == "Cobra Shot")
        //        {
        //            return PlayerFocus + 25;
        //        }

        //        if (Me.CastingSpell.Name == "Aimed Shot")
        //        {
        //            return PlayerFocus - 40;
        //        }
        //    }
        //    return PlayerFocus;
        //}

        #endregion

        #region Rapid Fire

        private Composite RapidFire()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.RapidFire &&
                SpellManager.HasSpell("Rapid Fire") &&
                !Me.HasAura("Rapid Fire") &&
                (THSettings.Instance.RapidFireCD ||
                 THSettings.Instance.RapidFireBurst &&
                 Burst) &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 40) &&
                SpellManager.CanCast("Rapid Fire"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Rapid Fire", Me, "RapidFire");
                        })
                );
        }

        #endregion

        #region Readiness

        private static bool ReadinessAM()
        {
            if (!THSettings.Instance.ReadinessAM ||
                !SpellManager.HasSpell("A Murder of Crows"))
            {
                return true;
            }

            if (SpellManager.HasSpell("A Murder of Crows") &&
                SpellManager.Spells["A Murder of Crows"].CooldownTimeLeft.TotalSeconds > 20)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessBW()
        {
            if (!THSettings.Instance.ReadinessBW ||
                !SpellManager.HasSpell("Bestial Wrath"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Bestial Wrath") &&
                !Me.HasAura("Bestial Wrath") &&
                SpellManager.Spells["Bestial Wrath"].CooldownTimeLeft.TotalSeconds > 20)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessLR()
        {
            if (!THSettings.Instance.ReadinessLR ||
                !SpellManager.HasSpell("Lynx Rush"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Lynx Rush") &&
                !Me.HasAura("Lynx Rush") &&
                SpellManager.Spells["Lynx Rush"].CooldownTimeLeft.TotalSeconds > 20 &&
                SpellManager.Spells["Lynx Rush"].CooldownTimeLeft.TotalSeconds < 75)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessRF()
        {
            if (!THSettings.Instance.ReadinessRF ||
                !SpellManager.HasSpell("Rapid Fire"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Rapid Fire") &&
                !Me.HasAura("Rapid Fire") &&
                SpellManager.Spells["Rapid Fire"].CooldownTimeLeft.TotalSeconds > 20)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessScS()
        {
            if (!THSettings.Instance.ReadinessScS ||
                !SpellManager.HasSpell("Scatter Shot"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Scatter Shot") &&
                SpellManager.Spells["Scatter Shot"].CooldownTimeLeft.TotalSeconds > 10)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessFT()
        {
            if (!THSettings.Instance.ReadinessFT ||
                !SpellManager.HasSpell("Freezing Trap"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Freezing Trap") &&
                SpellManager.Spells["Freezing Trap"].CooldownTimeLeft.TotalSeconds > 10)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessSiS()
        {
            if (!THSettings.Instance.ReadinessSiS ||
                !SpellManager.HasSpell("Silencing Shot"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Silencing Shot") &&
                SpellManager.Spells["Silencing Shot"].CooldownTimeLeft.TotalSeconds > 10)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessDe()
        {
            if (!THSettings.Instance.ReadinessDe ||
                !SpellManager.HasSpell("Deterrence"))
            {
                return true;
            }

            if (SpellManager.HasSpell("Deterrence") &&
                !Me.HasAura("Deterrence") &&
                SpellManager.Spells["Deterrence"].CooldownTimeLeft.TotalSeconds > 10)
            {
                return true;
            }
            return false;
        }

        private static bool ReadinessAllCD()
        {
            if (!THSettings.Instance.ReadinessBW &&
                !THSettings.Instance.ReadinessRF &&
                !THSettings.Instance.ReadinessScS &&
                !THSettings.Instance.ReadinessFT &&
                !THSettings.Instance.ReadinessSiS &&
                !THSettings.Instance.ReadinessDe)
            {
                return false;
            }
            return true;
        }

        private Composite Readiness()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Readiness &&
                SpellManager.HasSpell("Readiness") &&
                !Me.Mounted &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                ReadinessAllCD() &&
                SpellManager.CanCast("Readiness") &&
                ReadinessAM() &&
                ReadinessBW() &&
                ReadinessLR() &&
                ReadinessRF() &&
                ReadinessScS() &&
                ReadinessFT() &&
                ReadinessSiS() &&
                ReadinessDe(),
                new Action(
                    ret =>
                        {
                            CastSpell("Readiness", Me, "Readiness");
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region Roar of Sacrifice

        private Composite RoarofSacrifice()
        {
            return new Decorator(
                ret =>
                HoldPetAction < DateTime.Now &&
                Me.Combat &&
                PetCanCastAction("Roar of Sacrifice") &&
                !Invulnerable(Me) &&
                (THSettings.Instance.RoarofSacrifice &&
                 Me.Stunned ||
                 THSettings.Instance.RoarofSacrifice &&
                 Me.HealthPercent < THSettings.Instance.RoarofSacrificePercent ||
                 THSettings.Instance.RoarofSacrificeUnitMode &&
                 CountDPSTarget(Me) >= THSettings.Instance.RoarofSacrificeUnit),
                new Action(
                    ret =>
                        {
                            if (Me.Pet.Distance > 40 ||
                                !Me.Pet.InLineOfSpellSight)
                            {
                                HoldPetAction = DateTime.Now;
                                Logging.Write("PetFollow RoarofSacrifice");
                                Lua.DoString("RunMacroText('/PetPassive');");
                                Lua.DoString("RunMacroText('/PetFollow');");
                            }
                            else
                            {
                                PetCastAction("Roar of Sacrifice", Me);
                            }
                            return RunStatus.Failure;
                        })
                );
        }

        #endregion

        #region RestRotation

        private Composite RestRotation()
        {
            return new Decorator(
                ret =>
                !Me.IsDead &&
                !Me.IsGhost,
                new PrioritySelector(
                    //new Action(
                    //    delegate
                    //        {
                    //            Logging.Write("Consumable.GetBestFood(true) " + Consumable.GetBestFood(true));
                    //            Logging.Write("Consumable.GetBestDrink(true) " +
                    //                          Consumable.GetBestDrink(true));
                    //            return RunStatus.Failure;
                    //        }),
                    new Decorator(
                        ret =>
                        THSettings.Instance.UseFood &&
                        !Me.IsCasting &&
                        Me.IsMoving &&
                        !IsOverrideModeOn &&
                        Me.HealthPercent <= THSettings.Instance.UseFoodHP &&
                        !Me.IsSwimming &&
                        !Me.HasAura("Food") &&
                        Consumable.GetBestFood(true) != null,
                        new Action(delegate
                            {
                                Logging.Write("Using Food");
                                Thread.Sleep(TimeSpan.FromMilliseconds(300));

                                while (!IsOverrideModeOn && !Me.HasAura("Food") && !Me.Combat)
                                {
                                    WoWMovement.MoveStop();
                                    Styx.CommonBot.Rest.FeedImmediate();
                                    //StyxWoW.SleepForLagDuration();
                                }

                                if (Me.HasAura("Food") &&
                                    Me.HealthPercent <
                                    THSettings.Instance.DoNotHealAbove &&
                                    !Me.Combat)
                                {
                                    return RunStatus.Running;
                                }
                                return RunStatus.Success;
                            }
                            ))
                    ));
        }

        #endregion

        #region Stampede

        private Composite Stampede()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.Stampede &&
                SpellManager.HasSpell("Stampede") &&
                (THSettings.Instance.StampedeCD ||
                 THSettings.Instance.StampedeBurst &&
                 Burst) &&
                !Me.Mounted &&
                Me.Combat &&
                Me.GotAlivePet &&
                Attackable(Me.CurrentTarget, 40) &&
                (Me.CurrentTarget.IsPlayer ||
                 Me.CurrentTarget.CurrentHealth > Me.MaxHealth*10 &&
                 Me.CurrentTarget.MaxHealth > Me.MaxHealth*20 ||
                 IsDummy(Me.CurrentTarget)) &&
                SpellManager.CanCast("Stampede"),
                new Action(
                    ret =>
                        {
                            CastSpell("Stampede", Me.CurrentTarget, "Stampede");
                            return RunStatus.Failure;
                        })
                )
                ;
        }

        #endregion

        #region ScareBeast

        private static WoWUnit UnitScareBeast;

        private static bool GetUnitScareBeast()
        {
            UnitScareBeast = null;
            //Logging.Write("Looking for GetUnitScareBeast");

            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitScareBeast = (from unit in NearbyUnFriendlyUnits
                                  where unit.IsValid
                                  //where unit.IsPlayer
                                  where Attackable(unit, 30)
                                  //where unit.CurrentTarget != null
                                  //where unit.CurrentTarget.IsPlayer
                                  //where !IsEnemy(unit.CurrentTarget)
                                  //where unit.Class == WoWClass.Druid
                                  where
                                      unit.HasAura("Cat Form") ||
                                      unit.HasAura("Travel Form") ||
                                      unit.HasAura("Bear Form") ||
                                      unit.HasAura("Ghost Wolf") ||
                                      unit.IsBeast
                                  where !unit.HasAura("Scare Beast")
                                  orderby unit.MaxHealth descending
                                  orderby unit.CurrentHealth descending
                                  select unit).FirstOrDefault();
            }

            return UnitScareBeast != null;
        }

        private Composite ScareBeast()
        {
            return new Action(delegate
                {
                    if (THSettings.Instance.ScareBeastKey > 0 &&
                        SpellManager.HasSpell("Scare Beast") &&
                        GetAsyncKeyState(IndexToKeys(THSettings.Instance.ScareBeastKey)) < 0 &&
                        !Me.IsMoving &&
                        GetUnitScareBeast() &&
                        UnitScareBeast != null &&
                        UnitScareBeast.IsValid)
                    {
                        CastSpell("Scare Beast", UnitScareBeast, "ScareBeast");
                    }
                    return RunStatus.Failure;
                });
        }

        #endregion

        #region ScatterShotInterrupt

        private static WoWUnit UnitScatterShotInterrupt;

        private static bool GetUnitScatterShotInterrupt()
        {
            UnitScatterShotInterrupt = null;

            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    UnitScatterShotInterrupt = (from unit in NearbyUnFriendlyPlayers
                                                where unit.IsValid
                                                where
                                                    THSettings.Instance.InterruptTarget &&
                                                    Me.CurrentTarget != null &&
                                                    unit == Me.CurrentTarget 
                                                //where unit.IsPlayer
                                                where Attackable(unit, 20)
                                                where unit.HealthPercent > 20
                                                where Me.IsFacing(unit)
                                                where
                                                    unit.IsCasting &&
                                                    unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                    THSettings.Instance.ScatterShotInterruptMS + MyLatency &&
                                                    InterruptCastNoChannel(unit) > MyLatency ||
                                                    unit.IsChanneling &&
                                                    InterruptCastChannel(unit) > MyLatency
                                                orderby unit.CurrentCastTimeLeft ascending
                                                select unit).FirstOrDefault();
                }
                    //PvE Search
                else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
                {
                    UnitScatterShotInterrupt = (from unit in NearbyUnFriendlyUnits
                                                where unit.IsValid
                                                where
                                                    THSettings.Instance.InterruptTarget &&
                                                    Me.CurrentTarget != null &&
                                                    unit == Me.CurrentTarget 
                                                where Attackable(unit, 20)
                                                where !unit.IsBoss
                                                where unit.Combat
                                                where Me.IsFacing(unit)
                                                where
                                                    unit.IsCasting &&
                                                    unit.CanInterruptCurrentSpellCast &&
                                                    unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                    THSettings.Instance.ScatterShotInterruptMS + MyLatency + 1000 &&
                                                    unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency ||
                                                    unit.IsChanneling &&
                                                    unit.CanInterruptCurrentSpellCast &&
                                                    unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency
                                                orderby unit.CurrentCastTimeLeft ascending
                                                select unit).FirstOrDefault();
                }
            }

            return UnitScatterShotInterrupt != null;
        }

        private static void ScatterShotInterrupt()
        {
            if (Me.HasAura("Camouflage") &&
                (!Me.IsMoving ||
                 HasGlyph.Contains("119449"))) //Glyph of Camouflage - 119449)
            {
                return;
            }

            if (THSettings.Instance.ScatterShotInterrupt &&
                LastInterrupt + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                SpellManager.HasSpell("Scatter Shot") &&
                !Me.Mounted &&
                SpellManager.CanCast("Scatter Shot") &&
                GetUnitScatterShotInterrupt())
            {
                SpellManager.StopCasting();
                SafelyFacingTarget(UnitScatterShotInterrupt);

                while (UnitScatterShotInterrupt.CurrentCastTimeLeft.TotalMilliseconds >
                       THSettings.Instance.ScatterShotInterruptMS + MyLatency)
                {
                    Logging.Write("Waiting for Scatter Shot");
                }

                if (UnitScatterShotInterrupt.IsCasting || UnitScatterShotInterrupt.IsChanneling)
                {
                    CastSpell("Scatter Shot", UnitScatterShotInterrupt, "ScatterShotInterrupt");
                    LastInterrupt = DateTime.Now;
                    if (Me.CurrentTarget != null &&
                        (Me.CurrentTarget != UnitScatterShotInterrupt ||
                         Me.CurrentTarget == UnitScatterShotInterrupt &&
                         UnitScatterShotInterrupt.CurrentTarget == Me &&
                         UnitScatterShotInterrupt.HealthPercent > Me.HealthPercent) &&
                        !InvulnerableSpell(UnitScatterShotInterrupt))
                    {
                        LastScatterShot = DateTime.Now;
                        //LastCastontheGround + TimeSpan.FromMilliseconds(400)  < DateTime.Now &&
                        ScatterShotTrapUnit = UnitScatterShotInterrupt;
                        DoNotFreezingTrap = DateTime.Now;
                    }
                }
            }
        }

        #endregion

        #region ScatterShotFriendLowHP

        
        private static DateTime LastScatterShot;
        private static WoWUnit ScatterShotTrapUnit;

        #endregion

        #region SilencingShotInterrupt

        private static WoWUnit UnitSilencingShotInterrupt;
        private const int MyLatency = 300;

        private static bool GetUnitSilencingShotInterrupt()
        {
            UnitSilencingShotInterrupt = null;

            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsBattleground || Me.CurrentMap.IsArena)
                {
                    UnitSilencingShotInterrupt = (from unit in NearbyUnFriendlyPlayers
                                                  where unit.IsValid
                                                  where
                                                      THSettings.Instance.InterruptTarget &&
                                                      Me.CurrentTarget != null &&
                                                      unit == Me.CurrentTarget 
                                                  where Attackable(unit, 40)
                                                  where Me.IsFacing(unit)
                                                  where
                                                      unit.IsCasting &&
                                                      unit.CanInterruptCurrentSpellCast &&
                                                      unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                      THSettings.Instance.SilencingShotInterruptMS + MyLatency &&
                                                      InterruptCastNoChannel(unit) > MyLatency ||
                                                      unit.IsChanneling &&
                                                      unit.CanInterruptCurrentSpellCast &&
                                                      InterruptCastChannel(unit) > MyLatency
                                                  orderby unit.CurrentCastTimeLeft ascending
                                                  select unit).FirstOrDefault();
                }
                    //PvE Search
                else if (!Me.IsInInstance || Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid)
                {
                    UnitSilencingShotInterrupt = (from unit in NearbyUnFriendlyUnits
                                                  where unit.IsValid
                                                  where
                                                      THSettings.Instance.InterruptTarget &&
                                                      Me.CurrentTarget != null &&
                                                      unit == Me.CurrentTarget 
                                                  where Attackable(unit, 40)
                                                  where !unit.IsBoss
                                                  where unit.Combat
                                                  where Me.IsFacing(unit)
                                                  where
                                                      unit.IsCasting &&
                                                      unit.CanInterruptCurrentSpellCast &&
                                                      unit.CurrentCastTimeLeft.TotalMilliseconds <
                                                      THSettings.Instance.SilencingShotInterruptMS + MyLatency + 1000 &&
                                                      unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency ||
                                                      unit.IsChanneling &&
                                                      unit.CanInterruptCurrentSpellCast &&
                                                      unit.CurrentCastTimeLeft.TotalMilliseconds > MyLatency
                                                  orderby unit.CurrentCastTimeLeft ascending
                                                  select unit).FirstOrDefault();
                }
            }

            return UnitSilencingShotInterrupt != null;
        }

        private static DateTime LastInterrupt;

        private static void SilencingShotInterrupt()
        {
            if (Me.HasAura("Camouflage") &&
                (!Me.IsMoving ||
                 HasGlyph.Contains("119449"))) //Glyph of Camouflage - 119449)
            {
                return;
            }

            if (THSettings.Instance.SilencingShotInterrupt &&
                LastInterrupt + TimeSpan.FromMilliseconds(1000) < DateTime.Now &&
                !Me.Mounted &&
                SpellManager.HasSpell("Silencing Shot") &&
                SpellManager.CanCast("Silencing Shot") &&
                GetUnitSilencingShotInterrupt())
            {
                SpellManager.StopCasting();
                SafelyFacingTarget(UnitSilencingShotInterrupt);

                while (UnitSilencingShotInterrupt.CurrentCastTimeLeft.TotalMilliseconds >
                       THSettings.Instance.SilencingShotInterruptMS + MyLatency)
                {
                    Logging.Write("Waiting for Silencing Shot");
                }

                if (UnitSilencingShotInterrupt.IsCasting || UnitSilencingShotInterrupt.IsChanneling)
                {
                    CastSpell("Silencing Shot", UnitSilencingShotInterrupt, "SilencingShotInterrupt");
                    LastInterrupt = DateTime.Now;
                }
            }
        }

        #endregion

        #region Serpent Sting

        private static DateTime SerpentStingLastCast;

        private Composite SerpentSting()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SerpentSting &&
                SerpentStingLastCast + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                LastCastSpell != "Cobra Shot" &&
                LastCastSpell != "Steady Sting" &&
                SpellManager.HasSpell("Serpent Sting") &&
                !Me.Mounted &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                (Me.CurrentTarget.CurrentHealth > Me.MaxHealth*0.5 ||
                 IsDummy(Me.CurrentTarget)) &&
                MyAuraTimeLeft("Serpent Sting", Me.CurrentTarget) < 3000 &&
                SpellManager.CanCast("Serpent Sting"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            SerpentStingLastCast = DateTime.Now;
                            CastSpell("Serpent Sting", Me.CurrentTarget, "SerpentSting");
                        })
                );
        }

        #endregion

        #region SerpentStingImproved

        private static WoWUnit UnitSerpentStingImproved;

        private static bool GetUnitSerpentStingImproved()
        {
            UnitSerpentStingImproved = null;

            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitSerpentStingImproved = (from unit in NearbyUnFriendlyUnits
                                            where unit.IsValid
                                            //where unit != SerpentStingImprovedLastUnit
                                            where !unit.IsPet
                                            where unit != Me.CurrentTarget
                                            where Me.IsFacing(unit)
                                            where
                                                unit.CurrentHealth > Me.MaxHealth ||
                                                IsDummy(unit) ||
                                                unit.IsPlayer && unit.CurrentHealth > Me.MaxHealth*0.3
                                            where Attackable(unit, 40)
                                            where unit.Combat || IsDummy(unit)
                                            where
                                                unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                                unit.IsTargetingMeOrPet || IsDummy(unit)
                                            where !MyAura("Serpent Sting", unit)
                                            orderby unit.HealthPercent descending
                                            select unit).FirstOrDefault();
            }

            //if (UnitSerpentStingImproved != null)
            //{
            //    Logging.Write("Found UnitSerpentStingImproved");
            //}
            return UnitSerpentStingImproved != null;
        }

        private static DateTime SerpentStingImprovedLastCast;
        //private static WoWUnit SerpentStingImprovedLastUnit;

        private Composite SerpentStingImprovedBM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SerpentStingImproved &&
                //LastCastSpell != "Serpent Sting" &&
                SerpentStingImprovedLastCast + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                !Me.IsCasting &&
                //!Me.CurrentMap.IsBattleground &&
                //!Me.CurrentMap.IsArena &&
                SpellManager.HasSpell("Serpent Sting") &&
                Me.Combat &&
                !Me.Mounted &&
                //PlayerFocus > 40 &&
                Attackable(Me.CurrentTarget, 40) &&
                GetUnitSerpentStingImproved() &&
                UnitSerpentStingImproved != null &&
                UnitSerpentStingImproved.IsValid &&
                SpellManager.CanCast("Serpent Sting"), // &&
                //(SerpentStingImprovedLastCast + TimeSpan.FromMilliseconds(2000) < DateTime.Now ||
                // SerpentStingImprovedLastUnit != UnitSerpentStingImproved),
                new Action(
                    ret =>
                        {
                            SerpentStingImprovedLastCast = DateTime.Now;
                            //SafelyFacingTarget(UnitSerpentStingImproved);
                            CastSpell("Serpent Sting", UnitSerpentStingImproved, "SerpentStingImprovedBM");
                            //SerpentStingImprovedLastUnit = UnitSerpentStingImproved;
                        })
                );
        }

        private Composite SerpentStingImprovedSV()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SerpentStingImproved &&
                //LastCastSpell != "Serpent Sting" &&
                SerpentStingImprovedLastCast + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                !Me.IsCasting &&
                //!Me.CurrentMap.IsBattleground &&
                //!Me.CurrentMap.IsArena &&
                SpellManager.HasSpell("Serpent Sting") &&
                Me.Combat &&
                !Me.Mounted &&
                //PlayerFocus > 20 &&
                Attackable(Me.CurrentTarget, 40) &&
                GetUnitSerpentStingImproved() &&
                UnitSerpentStingImproved != null &&
                UnitSerpentStingImproved.IsValid &&
                SpellManager.CanCast("Serpent Sting"), // &&
                //(SerpentStingImprovedLastCast + TimeSpan.FromMilliseconds(2000) < DateTime.Now ||
                // SerpentStingImprovedLastUnit != UnitSerpentStingImproved),
                new Action(
                    ret =>
                        {
                            SerpentStingImprovedLastCast = DateTime.Now;
                            //SafelyFacingTarget(UnitSerpentStingImproved);
                            CastSpell("Serpent Sting", UnitSerpentStingImproved, "SerpentStingImprovedSV");
                            //SerpentStingImprovedLastUnit = UnitSerpentStingImproved;
                        })
                );
        }

        private Composite SerpentStingImprovedMM()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SerpentStingImproved &&
                //LastCastSpell != "Serpent Sting" &&
                SerpentStingImprovedLastCast + TimeSpan.FromMilliseconds(1500) < DateTime.Now &&
                !Me.IsCasting &&
                //!Me.CurrentMap.IsBattleground &&
                //!Me.CurrentMap.IsArena &&
                SpellManager.HasSpell("Serpent Sting") &&
                Me.Combat &&
                !Me.Mounted &&
                //PlayerFocus > 40 &&
                Attackable(Me.CurrentTarget, 50) &&
                GetUnitSerpentStingImproved() &&
                UnitSerpentStingImproved != null &&
                UnitSerpentStingImproved.IsValid && // &&
                SpellManager.CanCast("Serpent Sting"),
                //(SerpentStingImprovedLastCast + TimeSpan.FromMilliseconds(2000) < DateTime.Now ||
                // SerpentStingImprovedLastUnit != UnitSerpentStingImproved),
                new Action(
                    ret =>
                        {
                            SerpentStingImprovedLastCast = DateTime.Now;
                            //SafelyFacingTarget(UnitSerpentStingImproved);
                            CastSpell("Serpent Sting", UnitSerpentStingImproved, "SerpentStingImprovedMM");
                            //SerpentStingImprovedLastUnit = UnitSerpentStingImproved;
                        })
                );
        }

        #endregion

        #region Spirit Mend

        //private Composite SpiritMend()
        //{
        //    return new Decorator(
        //        ret =>
        //        Me.Combat &&
        //        Me.GotAlivePet &&
        //        (THSettings.Instance.SpiritMend &&
        //         Me.HealthPercent <= THSettings.Instance.SpiritMendMe ||
        //         THSettings.Instance.SpiritMendP &&
        //         Me.Pet.HealthPercent <= THSettings.Instance.SpiritMendPet) &&
        //        PetCanCastAction("Spirit Mend"),
        //        new Action(
        //            ret =>
        //                {
        //                    if (Me.HealthPercent <= THSettings.Instance.SpiritMendMe)
        //                    {
        //                        if (Me.Pet.Distance > 25 ||
        //                            !Me.Pet.InLineOfSpellSight)
        //                        {
        //                            Logging.Write("PetFollow to Spirit Mend");
        //                            Lua.DoString("RunMacroText('/PetPassive');");
        //                            Lua.DoString("RunMacroText('/PetFollow');");
        //                            PetFollowMode = true;
        //                        }
        //                        else
        //                        {
        //                            PetFollowMode = false;
        //                            Lua.DoString("RunMacroText('/cast [@player] Spirit Mend');");
        //                        }
        //                    }
        //                    else
        //                    {
        //                        Lua.DoString("RunMacroText('/cast [@pet] Spirit Mend');");
        //                    }
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        private static void SpiritMendVoid()
        {
            if (HoldPetAction < DateTime.Now &&
                //Me.Combat &&
                Me.GotAlivePet &&
                THSettings.Instance.SpiritMend &&
                Me.HealthPercent <= THSettings.Instance.SpiritMendMe &&
                PetCanCastAction("Spirit Mend"))
            {
                if (Me.Pet.Distance > 25 ||
                    !Me.Pet.InLineOfSpellSight)
                {
                    HoldPetAction = DateTime.Now;
                    Logging.Write("PetFollow to Spirit Mend Me");
                    Lua.DoString("RunMacroText('/PetPassive');");
                    Lua.DoString("RunMacroText('/PetFollow');");
                    Lua.DoString("RunMacroText('/cast [@pet] Dash');");
                }
                PetCastAction("Spirit Mend", Me);
                Logging.Write("PetFollow to Spirit Mend Me");
            }

            if (HoldPetAction < DateTime.Now &&
                //Me.Combat &&
                Me.GotAlivePet &&
                THSettings.Instance.SpiritMendP &&
                Me.Pet.HealthPercent <= THSettings.Instance.SpiritMendPet &&
                PetCanCastAction("Spirit Mend"))
            {
                PetCastAction("Spirit Mend", Me.Pet);
                Logging.Write("PetFollow to Spirit Mend Pet");
            }
        }

        #endregion

        #region SteadyShot

        private Composite SteadyShot()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SteadyShot &&
                LastCastSpell != "Fervor" &&
                SpellManager.HasSpell("Steady Shot") &&
                !Me.Mounted &&
                PlayerFocus < 70 &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Steady Shot", Me.CurrentTarget, false, false) &&
                !SpellManager.CanCast("Chimera Shot"),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Steady Shot", Me.CurrentTarget, "SteadyShot");
                        })
                );
        }

        #endregion

        #region SteadyShotRefreshSting

        //private static DateTime LastRefeshSting;

        //private Composite SteadyShotRefreshSting()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.SteadyShot &&
        //        SpellManager.HasSpell("Steady Shot") &&
        //        LastCastSpell != "Steady Shot" &&
        //        LastCastSpell != "Serpent Sting" &&
        //        LastRefeshSting + TimeSpan.FromMilliseconds(3000) < DateTime.Now &&
        //        !Me.Mounted &&
        //        PlayerFocus < BeastWithinFocus(75) &&
        //        Attackable(Me.CurrentTarget, 40) &&
        //        MyAuraTimeLeft("Serpent Sting", Me.CurrentTarget) > 3000 &&
        //        MyAuraTimeLeft("Serpent Sting", Me.CurrentTarget) < 9000 &&
        //        FacingOverride(Me.CurrentTarget) &&
        //        SpellManager.CanCast("Steady Shot", Me.CurrentTarget, false, false),
        //        new Action(
        //            ret =>
        //                {
        //                    LastRefeshSting = DateTime.Now;
        //                    SafelyFacingTarget(Me.CurrentTarget);
        //                    CastSpell("Steady Shot", Me.CurrentTarget, "SteadyShotRefreshSting");
        //                })
        //        );
        //}

        #endregion

        #region SteadyShotSteadyFocus

        private Composite SteadyShotSteadyFocus()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    THSettings.Instance.SteadyShot &&
                    SpellManager.HasSpell("Steady Shot") &&
                    !Me.Mounted &&
                    (Me.GetAuraById(53220) == null ||
                     Me.GetAuraById(53220) != null &&
                     Me.GetAuraById(53220).TimeLeft.TotalMilliseconds < 8000) &&
                    Attackable(Me.CurrentTarget, 40) &&
                    FacingOverride(Me.CurrentTarget),
                    new Action(
                        ret =>
                            {
                                //if (Me.GetAuraById(53220) != null)
                                //{
                                //    Logging.Write("TotalMilliseconds " +
                                //                  Me.GetAuraById(53220).TimeLeft.TotalMilliseconds);
                                //}
                                SafelyFacingTarget(Me.CurrentTarget);
                                CastSpell("Steady Shot", Me.CurrentTarget, "SteadyShotSteadyFocus");
                                while (Me.IsCasting)
                                {
                                }
                                //return Me.IsCasting ? RunStatus.Running : RunStatus.Success;
                            }))
                );
        }

        #endregion

        #region SteadyShotNoCobra

        private Composite SteadyShotNoCobra()
        {
            return new Decorator(
                ret =>
                THSettings.Instance.SteadyShot &&
                LastCastSpell != "Fervor" &&
                !SpellManager.HasSpell("Cobra Shot") &&
                SpellManager.HasSpell("Steady Shot") &&
                !Me.Mounted &&
                PlayerFocus < BeastWithinFocus(60) &&
                Attackable(Me.CurrentTarget, 40) &&
                FacingOverride(Me.CurrentTarget) &&
                SpellManager.CanCast("Steady Shot", Me.CurrentTarget, false, false),
                new Action(
                    ret =>
                        {
                            SafelyFacingTarget(Me.CurrentTarget);
                            CastSpell("Steady Shot", Me.CurrentTarget, "SteadyShotNoCobra");
                        })
                );
        }

        #endregion

        #region StopAttackOOC

        private Composite StopAttackOOC()
        {
            return new Decorator(
                ret =>
                !THSettings.Instance.AttackOOC &&
                !Me.Combat,
                new Action(
                    ret =>
                        {
                            Logging.Write("Stop Attack Out of Combat");
                            if (Me.IsAutoAttacking)
                            {
                                Lua.DoString("RunMacroText('/stopattack');");
                            }
                            return RunStatus.Success;
                        })
                );
        }

        #endregion

        #region StopCastingCheck

        private static void StopCastingCheck()
        {
            if ((Me.IsCasting &&
                 (Me.CastingSpellId == 109256 || //Power Shot
                  Me.CastingSpellId == 77767 || //Cobra
                  Me.CastingSpellId == 56641) || //Steady
                 Me.IsChanneling &&
                 Me.ChanneledCastingSpellId == 120360) && //Barrage
                !Attackable(Me.CurrentTarget, 40))
            {
                SpellManager.StopCasting();
                Logging.Write("StopCastingCheck");
            }
        }

        #endregion

        #region TargetFreezingTrap

        private static DateTime LastTargetFreezingTrap;

        private Composite TargetFreezingTrap()
        {
            return new Action(delegate
                {
                    if (THSettings.Instance.TargetFreezingTrap > 0 &&
                        SpellManager.HasSpell("Freezing Trap") &&
                        GetAsyncKeyState(IndexToKeys(THSettings.Instance.TargetFreezingTrap)) < 0 &&
                        AttackableNoCC(Me.CurrentTarget, 40) &&
                        GetSpellCooldown("Freezing Trap", 1000) <= TimeSpan.FromMilliseconds(400))
                    {
                        Lua.DoString("RunMacroText('/stopattack');");
                        Logging.Write("CC on Demand: TargetFreezingTrap");
                        SpellManager.StopCasting();
                        if (!Me.HasAura(77769)) //Trap Launcher
                        {
                            CastSpell("Trap Launcher", Me, "Trap Launcher: TargetFreezingTrap");
                        }

                        while (GetSpellCooldown("Freezing Trap") > TimeSpan.FromMilliseconds(1))
                        {
                            Logging.Write("CC on Demand: TargetFreezingTrap TreeRoot.Stop");
                        }
                        LastTargetFreezingTrap = DateTime.Now;
                        CastSpell("Freezing Trap", Me.CurrentTarget, "TargetFreezingTrap");

                        while (LastTargetFreezingTrap + TimeSpan.FromMilliseconds(300) > DateTime.Now &&
                               Me.CurrentPendingCursorSpell == null)
                        {
                            Logging.Write("Waiting for CurrentPendingCursorSpell");
                            return RunStatus.Running;
                        }
                        ObjectManager.Update();
                        SpellManager.ClickRemoteLocation(Me.CurrentTarget.Location);
                        return RunStatus.Success;
                    }
                    return RunStatus.Failure;
                });
        }

        #endregion

        #region TargetMyPetTarget

        private static WoWUnit UnitAttackingMyPet;

        private static bool GetUnitAttackingMyPet()
        {
            UnitAttackingMyPet = null;
            //using (StyxWoW.Memory.AcquireFrame())
            {
                UnitAttackingMyPet = (from unit in NearbyUnFriendlyUnits
                                      where unit.IsValid
                                      //orderby unit.HealthPercent ascending
                                      orderby unit.ThreatInfo.RawPercent descending
                                      where unit.Combat
                                      where unit.CurrentTarget != null
                                      where unit.CurrentTarget.IsValid
                                      where unit.CurrentTarget == Me.Pet
                                      select unit).FirstOrDefault();
            }
            return UnitAttackingMyPet != null;
        }

        private Composite TargetMyPetTarget()
        {
            return new Decorator(
                ret =>
                Me.Combat &&
                !Me.IsInInstance &&
                Me.GotAlivePet &&
                (Me.CurrentTarget == null ||
                 !Attackable(Me.CurrentTarget)) &&
                GetUnitAttackingMyPet(),
                new Action(delegate
                    {
                        Logging.Write(LogLevel.Diagnostic, "TargetMyPetTarget");
                        UnitAttackingMyPet.Target();
                        return RunStatus.Failure;
                    }))
                ;
        }

        #endregion

        #region TrapLauncher

        //private Composite TrapLauncher()
        //{
        //    return new Decorator(
        //        ret =>
        //        THSettings.Instance.TrapLauncher &&
        //        SpellManager.HasSpell("Trap Launcher") &&
        //        !Me.Mounted &&
        //        !Me.HasAura("Trap Launcher") &&
        //        SpellManager.CanCast("Trap Launcher"),
        //        new Action(
        //            ret =>
        //                {
        //                    CastSpell("Trap Launcher", Me, "TrapLauncher");
        //                    return RunStatus.Failure;
        //                })
        //        );
        //}

        #endregion

        #region UnitSpeed

        //private static double UnitSpeedCurrent;

        //private static double UnitSpeed()
        //{
        //    try
        //    {
        //        //using (StyxWoW.Memory.AcquireFrame())
        //        {
        //            if (target == Me)
        //            {
        //                UnitSpeedCurrent = Lua.GetReturnVal<int>("return GetUnitSpeed(\"Player\");", 1);
        //            }
        //            if (Me.CurrentTarget != null && target == Me.CurrentTarget)
        //            {
        //                UnitSpeedCurrent = Lua.GetReturnVal<int>("return GetUnitSpeed(\"Target\");", 1);
        //            }
        //            if (Me.FocusedUnit != null && target == Me.FocusedUnit)
        //            {
        //                UnitSpeedCurrent = Lua.GetReturnVal<int>("return GetUnitSpeed(\"Focus\");", 1);
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        Logging.Write(" Lua Failed in UnitSpeed");
        //        return 0;
        //    }
        //    return UnitSpeedCurrent;
        //}

        #endregion

        #region UseRacial

        private Composite UseRacial()
        {
            return new PrioritySelector(
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial &&
                    BreakCCLast + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                    DebuffCCDuration(Me, 3000) &&
                    !Me.HasAura("Sap") &&
                    SpellManager.HasSpell("Every Man for Himself") &&
                    SpellManager.CanCast("Every Man for Himself"),
                    new Action(delegate
                        {
                            if (THSettings.Instance.AutoTarget && Me.CurrentTarget == null &&
                                ValidUnit(MyLastTarget))
                            {
                                MyLastTarget.Target();
                            }

                            if (DebuffCCBreakonDamage(Me) &&
                                DebuffDot(Me))
                            {
                                Logging.Write("Waiting for Dot / Hand of Sacrifice break it!");
                            }
                            else
                            {
                                BreakCCLast = DateTime.Now;
                                Logging.Write("Use: Every Man for Himself");
                                CastSpell("Every Man for Himself", Me);
                            }
                            return RunStatus.Failure;
                        })),
                //Stoneform
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && Me.HealthPercent < THSettings.Instance.UrgentHeal &&
                    SpellManager.HasSpell("Stoneform") &&
                    SpellManager.CanCast("Stoneform"),
                    new Action(delegate
                        {
                            {
                                Logging.Write("Stoneform");
                                CastSpell("Stoneform", Me);
                            }
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && Me.HealthPercent < THSettings.Instance.UrgentHeal &&
                    SpellManager.HasSpell("Gift of the Naaru") &&
                    SpellManager.CanCast("Gift of the Naaru"),
                    new Action(delegate
                        {
                            {
                                Logging.Write("Gift of the Naaru");
                                CastSpell("Gift of the Naaru", Me);
                            }
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    Me.Combat &&
                    THSettings.Instance.AutoRacial && Me.ManaPercent < THSettings.Instance.PriorityHeal &&
                    SpellManager.HasSpell("Arcane Torrent") && SpellManager.HasSpell("Holy Insight") &&
                    (Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                    SpellManager.CanCast("Arcane Torrent"),
                    new Action(delegate
                        {
                            {
                                Logging.Write("Arcane Torrent");
                                CastSpell("Arcane Torrent", Me);
                            }
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoRacial &&
                    Burst &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    SpellManager.HasSpell("Blood Fury") &&
                    SpellManager.CanCast("Blood Fury"),
                    new Action(
                        ret =>
                            {
                                //Logging.Write(LogLevel.Diagnostic, "BloodFury");
                                CastSpell("Blood Fury", Me);
                                return RunStatus.Failure;
                            })),
                //Berserking
                new Decorator(
                    ret =>
                    THSettings.Instance.AutoRacial &&
                    Burst &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    SpellManager.HasSpell("Berserking") &&
                    SpellManager.CanCast("Berserking"),
                    new Action(
                        ret =>
                            {
                                //Logging.Write(LogLevel.Diagnostic, "BloodFury");
                                CastSpell("Berserking", Me);
                                return RunStatus.Failure;
                            }))
                );
        }

        #endregion

        #region UseTrinket

        private static DateTime BreakCCLast;

        private static void UseTrinket()
        {
            if (THSettings.Instance.Trinket1 == 1 &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                Me.IsFacing(Me.CurrentTarget) &&
                Me.Inventory.Equipped.Trinket1 != null &&
                CanUseEquippedItem(Me.Inventory.Equipped.Trinket1))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                              " on Cooldown");
                StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
            }
            else if (THSettings.Instance.Trinket1 == 2 &&
                     Me.Combat &&
                     Attackable(Me.CurrentTarget, 40) &&
                     Me.CurrentTarget.IsBoss &&
                     Me.IsFacing(Me.CurrentTarget) &&
                     Me.Inventory.Equipped.Trinket1 != null &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket1))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                              " on Cooldown (Boss Only)");
                StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
            }

            else if (THSettings.Instance.Trinket1 == 3 &&
                     Me.Combat &&
                     Burst &&
                     Attackable(Me.CurrentTarget, 40) &&
                     Me.Inventory.Equipped.Trinket1 != null &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket1))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                              " on Burst Mode");
                StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
            }
            else if (THSettings.Instance.Trinket1 == 4 &&
                     BreakCCLast + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                     Me.Combat &&
                     Me.Inventory.Equipped.Trinket1 != null &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket1) &&
                     DebuffCCDuration(Me, 3000) && !Me.HasAura("Sap"))
            {
                BreakCCLast = DateTime.Now;
                StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                              " on Lose Control");
            }
            else if (THSettings.Instance.Trinket1 == 5 &&
                     Me.Combat &&
                     Me.Inventory.Equipped.Trinket1 != null &&
                     Me.HealthPercent < THSettings.Instance.Trinket1HP &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket1))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                              " on Low HP");
                StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
            }
            else if (THSettings.Instance.Trinket1 == 6 &&
                     Me.Combat &&
                     Me.Inventory.Equipped.Trinket1 != null &&
                     Attackable(Me.CurrentTarget, 40) &&
                     Me.CurrentTarget.HealthPercent < THSettings.Instance.Trinket1HP &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket1))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name +
                              " on Enemy Unit Low HP");
                StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
            }

            //Trinket 2
            if (THSettings.Instance.Trinket2 == 1 &&
                Me.Combat &&
                Attackable(Me.CurrentTarget, 40) &&
                Me.IsFacing(Me.CurrentTarget) &&
                Me.Inventory.Equipped.Trinket2 != null &&
                CanUseEquippedItem(Me.Inventory.Equipped.Trinket2))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                              " on Cooldown");
                StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
            }
            else if (THSettings.Instance.Trinket2 == 2 &&
                     Me.Combat &&
                     Attackable(Me.CurrentTarget, 40) &&
                     Me.CurrentTarget.IsBoss &&
                     Me.IsFacing(Me.CurrentTarget) &&
                     Me.Inventory.Equipped.Trinket2 != null &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket2))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                              " on Cooldown (Boss Only)");
                StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
            }

            else if (THSettings.Instance.Trinket2 == 3 &&
                     Me.Combat &&
                     Burst &&
                     Attackable(Me.CurrentTarget, 40) &&
                     Me.Inventory.Equipped.Trinket2 != null &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket2))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                              " on Burst Mode");
                StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
            }
            else if (THSettings.Instance.Trinket2 == 4 &&
                     BreakCCLast + TimeSpan.FromMilliseconds(2000) < DateTime.Now &&
                     Me.Combat &&
                     Me.Inventory.Equipped.Trinket2 != null &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket2) &&
                     DebuffCCDuration(Me, 3000) && !Me.HasAura("Sap"))
            {
                BreakCCLast = DateTime.Now;
                StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                              " on Lose Control");
            }
            else if (THSettings.Instance.Trinket2 == 5 &&
                     Me.Combat &&
                     Me.Inventory.Equipped.Trinket2 != null &&
                     Me.HealthPercent < THSettings.Instance.Trinket2HP &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket2))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                              " on Low HP");
                StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
            }
            else if (THSettings.Instance.Trinket2 == 6 &&
                     Me.Combat &&
                     Me.Inventory.Equipped.Trinket2 != null &&
                     Attackable(Me.CurrentTarget, 40) &&
                     Me.CurrentTarget.HealthPercent < THSettings.Instance.Trinket2HP &&
                     CanUseEquippedItem(Me.Inventory.Equipped.Trinket2))
            {
                Logging.Write("Activate " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name +
                              " on Enemy Unit Low HP");
                StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
            }
        }

        #endregion

        #region UseProfession

        private Composite UseProfession()
        {
            return new PrioritySelector(
                //Engineering
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 1 &&
                    Me.Inventory.Equipped.Hands != null &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                        {
                            Logging.Write("Use: Gloves Buff Activated on Cooldown");
                            StyxWoW.Me.Inventory.Equipped.Hands.Use();
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 2 &&
                    Me.Inventory.Equipped.Hands != null &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    Me.CurrentTarget.IsBoss &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                        {
                            Logging.Write("Use: Gloves Buff Activated on Cooldown (Boss Only)");
                            StyxWoW.Me.Inventory.Equipped.Hands.Use();
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 3 &&
                    Burst &&
                    Me.Inventory.Equipped.Hands != null &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                        {
                            Logging.Write("Use: Gloves Buff Activated on Burst Mode");
                            StyxWoW.Me.Inventory.Equipped.Hands.Use();
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 4 &&
                    Me.Inventory.Equipped.Hands != null &&
                    Me.Combat &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                        {
                            Logging.Write("Use: Gloves Buff Activated on Lose Control");
                            StyxWoW.Me.Inventory.Equipped.Hands.Use();
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 5 &&
                    Me.Inventory.Equipped.Hands != null &&
                    Me.Combat &&
                    Me.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                        {
                            Logging.Write("Use: GLoves Buff Activated on Low HP");
                            StyxWoW.Me.Inventory.Equipped.Hands.Use();
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 6 &&
                    Me.Inventory.Equipped.Hands != null &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    Me.CurrentTarget.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    !Me.CurrentTarget.IsPet &&
                    CanUseEquippedItem(Me.Inventory.Equipped.Hands),
                    new Action(delegate
                        {
                            Logging.Write("Use: Gloves Buff Activated on Enemy Unit Low HP");
                            StyxWoW.Me.Inventory.Equipped.Hands.Use();
                            return RunStatus.Failure;
                        })),
                //Herbalism
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 1 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    SpellManager.CanCast("Lifeblood"),
                    new Action(delegate
                        {
                            Logging.Write("Use: Lifeblood Activated on Cooldown");
                            CastSpell("Lifeblood", Me);
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 2 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    SpellManager.CanCast("Lifeblood"),
                    new Action(delegate
                        {
                            Logging.Write("Use: Lifeblood Activated on Cooldown (Boss Only)");
                            CastSpell("Lifeblood", Me);
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 3 &&
                    Burst &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat && Attackable(Me.CurrentTarget, 40) &&
                    SpellManager.CanCast("Lifeblood"),
                    new Action(delegate
                        {
                            Logging.Write("Use: Lifeblood Activated on Burst Mode");
                            CastSpell("Lifeblood", Me);
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 4 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    SpellManager.CanCast("Lifeblood") &&
                    DebuffCCDuration(Me, 3000),
                    new Action(delegate
                        {
                            Logging.Write("Use: Lifeblood Activated on Lose Control");
                            CastSpell("Lifeblood", Me);
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 5 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    SpellManager.CanCast("Lifeblood") &&
                    Me.HealthPercent < THSettings.Instance.ProfBuffHP,
                    new Action(delegate
                        {
                            Logging.Write("Use: Lifeblood Activated on Low HP");
                            CastSpell("Lifeblood", Me);
                            return RunStatus.Failure;
                        })),
                new Decorator(
                    ret =>
                    THSettings.Instance.ProfBuff == 6 &&
                    SpellManager.HasSpell("Lifeblood") &&
                    Me.Combat &&
                    Attackable(Me.CurrentTarget, 40) &&
                    !Me.CurrentTarget.IsPet &&
                    Me.CurrentTarget.HealthPercent < THSettings.Instance.ProfBuffHP &&
                    SpellManager.CanCast("Lifeblood"),
                    new Action(delegate
                        {
                            Logging.Write("Use: Lifeblood Activated on Enemy Unit Low HP");
                            CastSpell("Lifeblood", Me, "Use: Lifeblood Activated on Enemy Unit Low HP");
                            return RunStatus.Failure;
                        }))
                );
        }

        #endregion

        #region WriteDebug

        private Composite WriteDebug(string message)
        {
            return new Action(delegate
                {
                    Logging.Write(message);
                    return RunStatus.Failure;
                });
        }

        #endregion

        #region WriteTargetInfo

        private static DateTime LastWriteTargetInfo;

        private static void WriteTargetInfo()
        {
            if (LastWriteTargetInfo + TimeSpan.FromMilliseconds(3000) < DateTime.Now)
            {
                WoWUnit target;
                if (Me.CurrentTarget != null)
                {
                    target = Me.CurrentTarget;
                }
                else
                {
                    target = Me;
                }

                Logging.Write("WriteTargetInfo");
                Logging.Write(target.Name +
                              " Entry: " + target.Entry +
                              " Dist: " + Math.Round(target.Distance, 1) +
                              " Dist2D: " + Math.Round(target.Distance2D, 1) +
                              " BoundingRadius: " +
                              Math.Round(target.BoundingRadius, 1) +
                              " BoundingHeight: " +
                              Math.Round(target.BoundingHeight, 1) +
                              " IsWithinMeleeRange: " + target.IsWithinMeleeRange);
                if (target.IsCasting)
                {
                    Logging.Write("IsCasting: " + target.CastingSpell.Name + " ID: " + target.CastingSpell.Id);
                }

                if (target.IsChanneling)
                {
                    Logging.Write("IsChanneling: " + target.ChanneledSpell.Name + " ID: " + target.ChanneledSpell.Id);
                }


                LastWriteTargetInfo = DateTime.Now;
            }
        }

        #endregion
    }
}